Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WALWAL-106] fixtureMonkey 도입 #28

Merged
merged 4 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ out/
### Custom ###
*.env
*.DS_Store
/src/main/generated
/src/main/generated
.*-database
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
spring:
profiles:
group:
test: "test"
application:
name: stonebed

Expand Down
Original file line number Diff line number Diff line change
@@ -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,
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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<String> addressList;

public enum Gender {
MAN,
WOMAN
}
}
12 changes: 12 additions & 0 deletions src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -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
Loading