Skip to content

Commit

Permalink
feat: add test data load
Browse files Browse the repository at this point in the history
this is for demo usage of Profile & ConditionalOnProperty features
  • Loading branch information
yingtingxu committed Jun 5, 2024
1 parent 7e7b10a commit 01544c8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mall-cloud-native/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ dependencies {
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

bootRun {
systemProperty "mall.testdata.enabled", "true"
}

tasks.named('test') {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.mall.catalogservice.demo;

import org.mall.catalogservice.domain.Book;
import org.mall.catalogservice.domain.BookRepository;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Profile;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
//@Profile("testdata")
@ConditionalOnProperty(name="mall.testdata.enabled", havingValue = "true")
public class BookDataLoader {
private final BookRepository bookRepository;

public BookDataLoader(BookRepository bookRepository) {
this.bookRepository = bookRepository;
}

@EventListener(ApplicationReadyEvent.class)
public void loadBookTestData() {
var book1 = new Book("1234567891", "Northern Lights", "Lyra Silverstar", 9.90);
var book2 = new Book("1234567892", "Polar Journey", "Iorek Polarson", 12.90);
bookRepository.save(book1);
bookRepository.save(book2);
}
}

0 comments on commit 01544c8

Please sign in to comment.