diff --git a/.gitignore b/.gitignore index 483ba31b..acb28ecc 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,5 @@ out/ ### Custom ### *.env *.DS_Store -/src/main/generated \ No newline at end of file +/src/main/generated +.*-database diff --git a/build.gradle b/build.gradle index bccaa615..6621b107 100644 --- a/build.gradle +++ b/build.gradle @@ -36,6 +36,7 @@ dependencies { implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0' compileOnly 'org.projectlombok:lombok' + // Database runtimeOnly 'com.h2database:h2' runtimeOnly 'com.mysql:mysql-connector-j' @@ -46,14 +47,21 @@ dependencies { annotationProcessor 'jakarta.annotation:jakarta.annotation-api' annotationProcessor 'jakarta.persistence:jakarta.persistence-api' + // Lombok annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' + // Test Lombok + testCompileOnly 'org.projectlombok:lombok' + testAnnotationProcessor 'org.projectlombok:lombok' + // FixtureMonkey + testImplementation("com.navercorp.fixturemonkey:fixture-monkey-starter:1.0.20") } tasks.named('test') { useJUnitPlatform() + jvmArgs '-Xshare:off' // JVM 아규먼트 설정 finalizedBy jacocoTestReport } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index c55888eb..4b536bcf 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,4 +1,7 @@ spring: + profiles: + group: + test: "test" application: name: stonebed diff --git a/src/test/java/com/depromeet/stonebed/fixtureMonkeyTest/Order.java b/src/test/java/com/depromeet/stonebed/fixtureMonkeyTest/Order.java new file mode 100644 index 00000000..247171d9 --- /dev/null +++ b/src/test/java/com/depromeet/stonebed/fixtureMonkeyTest/Order.java @@ -0,0 +1,43 @@ +package com.depromeet.stonebed.fixtureMonkeyTest; + +import jakarta.validation.constraints.Max; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.PastOrPresent; +import jakarta.validation.constraints.Size; +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; +import lombok.Data; + +@Data +public class Order { + @NotNull private Long id; + + @NotBlank private String orderNo; + + private OrderType orderType; + + @Size(min = 2, max = 10) + private String productName; + + @Min(1) + @Max(100) + private int quantity; + + @Min(0) + private long price; + + private long totalPrice; + + private List<@NotBlank @Size(max = 10) String> items = new ArrayList<>(); + + @PastOrPresent private Instant orderedAt; + + public enum OrderType { + SMARTSTORE, + BLOG, + CAFE, + } +} diff --git a/src/test/java/com/depromeet/stonebed/fixtureMonkeyTest/TestFixtureMonkey.java b/src/test/java/com/depromeet/stonebed/fixtureMonkeyTest/TestFixtureMonkey.java new file mode 100644 index 00000000..ba452a1a --- /dev/null +++ b/src/test/java/com/depromeet/stonebed/fixtureMonkeyTest/TestFixtureMonkey.java @@ -0,0 +1,69 @@ +package com.depromeet.stonebed.fixtureMonkeyTest; + +import static org.assertj.core.api.BDDAssertions.*; + +import com.navercorp.fixturemonkey.FixtureMonkey; +import com.navercorp.fixturemonkey.api.introspector.FieldReflectionArbitraryIntrospector; +import net.jqwik.api.Arbitraries; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class TestFixtureMonkey { + + @Test + void checkPerson() { + // given + FixtureMonkey sut = + FixtureMonkey.builder() + .objectIntrospector(FieldReflectionArbitraryIntrospector.INSTANCE) + .defaultNotNull(true) + .build(); + + // when + User person = + sut.giveMeBuilder(User.class) + .set("age", Arbitraries.integers().between(1, 100)) + .set("personNo", Arbitraries.strings().ofMinLength(1).ofMaxLength(16)) + .sample(); + + // then + then(person.getPersonId()).isNotNull(); + then(person.getPersonName()).isNotBlank(); + then(person.getPersonNo().length()).isBetween(1, 16); + } + + @Test + void testOrder() { + // given + FixtureMonkey sut = + FixtureMonkey.builder() + .objectIntrospector(FieldReflectionArbitraryIntrospector.INSTANCE) + .defaultNotNull(true) + .build(); + + // when + Order actual = + sut.giveMeBuilder(Order.class) + .set("productName", Arbitraries.strings().ofMinLength(2).ofMaxLength(10)) + .set("price", Arbitraries.longs().between(0, 1000)) + .set("quantity", Arbitraries.integers().between(1, 100)) + .set( + "items", + Arbitraries.strings() + .ofMaxLength(10) + .list() + .ofMinSize(1) + .ofMaxSize(2)) + .sample(); + + // then + then(actual.getId()).isNotNull(); // @NotNull + then(actual.getOrderNo()).isNotBlank(); // @NotBlank + then(actual.getProductName().length()).isBetween(2, 10); // @Size(min = 2, max = 10) + then(actual.getQuantity()).isBetween(1, 100); // Min(1) @Max(100) + then(actual.getPrice()).isGreaterThanOrEqualTo(0); // @Min(0) + then(actual.getItems()).hasSizeLessThan(3); // @Size(max = 3) + then(actual.getItems()).allMatch(it -> it.length() <= 10); // @NotBlank @Size(max = 10) + } +} diff --git a/src/test/java/com/depromeet/stonebed/fixtureMonkeyTest/User.java b/src/test/java/com/depromeet/stonebed/fixtureMonkeyTest/User.java new file mode 100644 index 00000000..069aa5fb --- /dev/null +++ b/src/test/java/com/depromeet/stonebed/fixtureMonkeyTest/User.java @@ -0,0 +1,30 @@ +package com.depromeet.stonebed.fixtureMonkeyTest; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; +import java.util.List; +import lombok.Data; + +@Data +public class User { + + @NotNull private String personId; + + @NotBlank private String personName; + + private Gender gender; + + @Size(min = 1, max = 16) + private String personNo; + + @Size(min = 1, max = 16) + private int age; + + private List addressList; + + public enum Gender { + MAN, + WOMAN + } +} diff --git a/src/test/resources/application-test.yml b/src/test/resources/application-test.yml new file mode 100644 index 00000000..a13f2fde --- /dev/null +++ b/src/test/resources/application-test.yml @@ -0,0 +1,12 @@ +spring: + config: + activate: + on-profile: "test" + + datasource: + url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false;MODE=MYSQL + + jpa: + properties: + hibernate: + default_batch_fetch_size: 1000