From f08d20926e1d38eef72d32c85fd27c1de84f9724 Mon Sep 17 00:00:00 2001 From: DongminL Date: Fri, 6 Dec 2024 17:24:50 +0900 Subject: [PATCH 01/22] =?UTF-8?q?docs:=20Swagger=20=EC=96=B4=EB=85=B8?= =?UTF-8?q?=ED=85=8C=EC=9D=B4=EC=85=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/shop/admin/controller/AdminOrderController.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/example/shop/admin/controller/AdminOrderController.java b/src/main/java/com/example/shop/admin/controller/AdminOrderController.java index 60a6937..bd01166 100644 --- a/src/main/java/com/example/shop/admin/controller/AdminOrderController.java +++ b/src/main/java/com/example/shop/admin/controller/AdminOrderController.java @@ -7,11 +7,13 @@ import com.example.shop.admin.service.AdminOrderService; import com.example.shop.common.dto.PageResponse; import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +@Tag(name="Admin Order") @RestController @RequestMapping("/api/admin/orders") @RequiredArgsConstructor From 6f3e651445a769d430becf2d57230bae6ee66050 Mon Sep 17 00:00:00 2001 From: DongminL Date: Sat, 7 Dec 2024 10:19:35 +0900 Subject: [PATCH 02/22] =?UTF-8?q?test:=20Email=20Test,=20TestContainers,?= =?UTF-8?q?=20H2=20=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index 869139f..7fbace4 100644 --- a/build.gradle +++ b/build.gradle @@ -36,35 +36,41 @@ dependencies { // Mail implementation 'org.springframework.boot:spring-boot-starter-mail' + testImplementation 'com.icegreen:greenmail-junit5:2.1.1' + testImplementation 'commons-codec:commons-codec:1.17.1' // Security implementation 'org.springframework.boot:spring-boot-starter-security' // DB implementation 'org.mariadb.jdbc:mariadb-java-client:3.1.2' + testRuntimeOnly 'com.h2database:h2' - // Redis 추가 + // Redis implementation 'org.springframework.boot:spring-boot-starter-data-redis' // Swagger implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0' - //jwt + // JWT implementation 'io.jsonwebtoken:jjwt-api:0.11.5' implementation 'io.jsonwebtoken:jjwt-impl:0.11.5' implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5' - // mybatis + // MyBatis implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.4' - // lombok + // Lombok annotationProcessor 'org.projectlombok:lombok' compileOnly 'org.projectlombok:lombok' - // test + // Test testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.security:spring-security-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' + testImplementation 'org.springframework.boot:spring-boot-testcontainers' + testImplementation 'org.testcontainers:testcontainers' + testImplementation 'org.testcontainers:junit-jupiter' //Querydsl 추가 implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta' From 88403dfb95dcf2c700640742924d4b7ff8c41fbf Mon Sep 17 00:00:00 2001 From: DongminL Date: Sat, 7 Dec 2024 10:32:38 +0900 Subject: [PATCH 03/22] =?UTF-8?q?test:=20Test=20Profile=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95=EC=97=90=EC=84=9C=20=EC=A0=9C=EC=99=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/shop/global/config/db/RedisConfig.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/example/shop/global/config/db/RedisConfig.java b/src/main/java/com/example/shop/global/config/db/RedisConfig.java index eb67d36..54cbc06 100644 --- a/src/main/java/com/example/shop/global/config/db/RedisConfig.java +++ b/src/main/java/com/example/shop/global/config/db/RedisConfig.java @@ -4,6 +4,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisStandaloneConfiguration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; @@ -12,6 +13,7 @@ import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; +@Profile("!test") @Configuration @EnableRedisRepositories public class RedisConfig { From 7554ae31b54880641503c758e6b6913bc02fca1c Mon Sep 17 00:00:00 2001 From: DongminL Date: Sat, 7 Dec 2024 11:52:35 +0900 Subject: [PATCH 04/22] =?UTF-8?q?test:=20=EA=B0=81=20=EB=8B=A8=EA=B3=84?= =?UTF-8?q?=EB=B3=84=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=85=8B=ED=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/annotation/ControllerTest.java | 63 +++++++++++++++++++ .../shop/global/annotation/RedisTest.java | 13 ++++ .../global/annotation/RepositoryTest.java | 9 +++ .../shop/global/annotation/ServiceTest.java | 13 ++++ 4 files changed, 98 insertions(+) create mode 100644 src/test/java/com/example/shop/global/annotation/ControllerTest.java create mode 100644 src/test/java/com/example/shop/global/annotation/RedisTest.java create mode 100644 src/test/java/com/example/shop/global/annotation/RepositoryTest.java create mode 100644 src/test/java/com/example/shop/global/annotation/ServiceTest.java diff --git a/src/test/java/com/example/shop/global/annotation/ControllerTest.java b/src/test/java/com/example/shop/global/annotation/ControllerTest.java new file mode 100644 index 0000000..f2302b9 --- /dev/null +++ b/src/test/java/com/example/shop/global/annotation/ControllerTest.java @@ -0,0 +1,63 @@ +package com.example.shop.global.annotation; + +import com.example.shop.admin.controller.AdminOrderController; +import com.example.shop.admin.controller.AdminProductController; +import com.example.shop.admin.service.AdminOrderService; +import com.example.shop.admin.service.AdminProductService; +import com.example.shop.auth.controller.AuthController; +import com.example.shop.auth.service.AuthService; +import com.example.shop.global.config.auth.JwtAuthenticationFilter; +import com.example.shop.user.controller.CartController; +import com.example.shop.user.controller.OrderController; +import com.example.shop.user.controller.ProductController; +import com.example.shop.user.service.CartService; +import com.example.shop.user.service.OrderService; +import com.example.shop.user.service.ProductService; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.web.servlet.MockMvc; + +@ActiveProfiles("test") +@ExtendWith(MockitoExtension.class) +@WebMvcTest({ + AdminOrderController.class, + AdminProductController.class, + AuthController.class, + CartController.class, + OrderController.class, + ProductController.class +}) +public abstract class ControllerTest { + + @Autowired + protected MockMvc mockMvc; + + @Autowired + protected ObjectMapper objectMapper; + + @MockBean + protected JwtAuthenticationFilter jwtAuthenticationFilter; + + @MockBean + protected AdminOrderService adminOrderService; + + @MockBean + protected AdminProductService adminProductService; + + @MockBean + protected AuthService authService; + + @MockBean + protected CartService cartService; + + @MockBean + protected OrderService orderService; + + @MockBean + protected ProductService productService; +} diff --git a/src/test/java/com/example/shop/global/annotation/RedisTest.java b/src/test/java/com/example/shop/global/annotation/RedisTest.java new file mode 100644 index 0000000..fbca1fe --- /dev/null +++ b/src/test/java/com/example/shop/global/annotation/RedisTest.java @@ -0,0 +1,13 @@ +package com.example.shop.global.annotation; + +import com.example.shop.global.config.RedisTestConfig; +import com.example.shop.global.config.RedisTestContainer; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.ActiveProfiles; + +@SpringBootTest +@ActiveProfiles("test") +@Import(RedisTestConfig.class) +public abstract class RedisTest extends RedisTestContainer { +} diff --git a/src/test/java/com/example/shop/global/annotation/RepositoryTest.java b/src/test/java/com/example/shop/global/annotation/RepositoryTest.java new file mode 100644 index 0000000..b0e6141 --- /dev/null +++ b/src/test/java/com/example/shop/global/annotation/RepositoryTest.java @@ -0,0 +1,9 @@ +package com.example.shop.global.annotation; + +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.ActiveProfiles; + +@DataJpaTest +@ActiveProfiles("test") +public abstract class RepositoryTest { +} diff --git a/src/test/java/com/example/shop/global/annotation/ServiceTest.java b/src/test/java/com/example/shop/global/annotation/ServiceTest.java new file mode 100644 index 0000000..c0adbad --- /dev/null +++ b/src/test/java/com/example/shop/global/annotation/ServiceTest.java @@ -0,0 +1,13 @@ +package com.example.shop.global.annotation; + +import com.example.shop.global.config.RedisTestConfig; +import com.example.shop.global.config.RedisTestContainer; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.ActiveProfiles; + +@SpringBootTest +@ActiveProfiles("test") +@Import(RedisTestConfig.class) +public abstract class ServiceTest extends RedisTestContainer { +} From 87636c059bbc8922078b91d6a897ea6054667609 Mon Sep 17 00:00:00 2001 From: DongminL Date: Sat, 7 Dec 2024 11:53:32 +0900 Subject: [PATCH 05/22] =?UTF-8?q?refactor:=20JPA=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=8B=9C,=20=EC=98=A4=EB=A5=98=20=EC=98=88?= =?UTF-8?q?=EB=B0=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/example/shop/ShopApplication.java | 1 - .../com/example/shop/global/config/db/JpaConfig.java | 9 +++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/example/shop/global/config/db/JpaConfig.java diff --git a/src/main/java/com/example/shop/ShopApplication.java b/src/main/java/com/example/shop/ShopApplication.java index 5cfd1e2..99cefdc 100644 --- a/src/main/java/com/example/shop/ShopApplication.java +++ b/src/main/java/com/example/shop/ShopApplication.java @@ -7,7 +7,6 @@ import org.springframework.data.jpa.repository.config.EnableJpaAuditing; @SpringBootApplication -@EnableJpaAuditing @PropertySource("classpath:security.properties") public class ShopApplication { diff --git a/src/main/java/com/example/shop/global/config/db/JpaConfig.java b/src/main/java/com/example/shop/global/config/db/JpaConfig.java new file mode 100644 index 0000000..5490473 --- /dev/null +++ b/src/main/java/com/example/shop/global/config/db/JpaConfig.java @@ -0,0 +1,9 @@ +package com.example.shop.global.config.db; + +import org.springframework.context.annotation.Configuration; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; + +@Configuration +@EnableJpaAuditing +public class JpaConfig { +} From 857f92ae0a916709f35d973d669e8075001e38d6 Mon Sep 17 00:00:00 2001 From: DongminL Date: Sat, 7 Dec 2024 11:54:47 +0900 Subject: [PATCH 06/22] =?UTF-8?q?test:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=ED=99=98=EA=B2=BD=EC=97=90=EC=84=9C=20Redis=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shop/global/config/RedisTestConfig.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/test/java/com/example/shop/global/config/RedisTestConfig.java diff --git a/src/test/java/com/example/shop/global/config/RedisTestConfig.java b/src/test/java/com/example/shop/global/config/RedisTestConfig.java new file mode 100644 index 0000000..e4e8411 --- /dev/null +++ b/src/test/java/com/example/shop/global/config/RedisTestConfig.java @@ -0,0 +1,30 @@ +package com.example.shop.global.config; + +import com.example.shop.admin.dto.OrderDeliveryRequest; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.repository.configuration.EnableRedisRepositories; +import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +@TestConfiguration +@EnableRedisRepositories +public class RedisTestConfig { + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(connectionFactory); + + // JSON 형식으로 직렬화 + Jackson2JsonRedisSerializer serializer = new Jackson2JsonRedisSerializer<>(OrderDeliveryRequest.class); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(serializer); + redisTemplate.setHashKeySerializer(new StringRedisSerializer()); + redisTemplate.setHashValueSerializer(serializer); + + return redisTemplate; + } +} From e3017878c8336684e516107719e4239c154f0b43 Mon Sep 17 00:00:00 2001 From: DongminL Date: Sat, 7 Dec 2024 11:55:06 +0900 Subject: [PATCH 07/22] =?UTF-8?q?test:=20Redis=20=EC=82=AC=EC=9A=A9=20?= =?UTF-8?q?=EC=8B=9C=20TestContainer=EB=A5=BC=20=EC=82=AC=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/config/RedisTestContainer.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/test/java/com/example/shop/global/config/RedisTestContainer.java diff --git a/src/test/java/com/example/shop/global/config/RedisTestContainer.java b/src/test/java/com/example/shop/global/config/RedisTestContainer.java new file mode 100644 index 0000000..bbd2640 --- /dev/null +++ b/src/test/java/com/example/shop/global/config/RedisTestContainer.java @@ -0,0 +1,28 @@ +package com.example.shop.global.config; + +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.utility.DockerImageName; + +@Testcontainers +public class RedisTestContainer { + + private static final String REDIS_IMAGE = "redis:7.4.1-alpine"; + private static final int REDIS_PORT = 6379; + private static final GenericContainer REDIS_CONTAINER; + + static { + REDIS_CONTAINER = new GenericContainer<>(DockerImageName.parse(REDIS_IMAGE)) + .withExposedPorts(REDIS_PORT) + .withReuse(true); + REDIS_CONTAINER.start(); + } + + @DynamicPropertySource + private static void registerRedisProperties(DynamicPropertyRegistry registry) { + registry.add("spring.data.redis.host", REDIS_CONTAINER::getHost); + registry.add("spring.data.redis.port", () -> REDIS_CONTAINER.getMappedPort(REDIS_PORT)); + } +} From 1b30562ed4bbf1a77c6d390bde59c2cf763bf871 Mon Sep 17 00:00:00 2001 From: DongminL Date: Sat, 7 Dec 2024 11:55:36 +0900 Subject: [PATCH 08/22] =?UTF-8?q?test:=20=EC=9D=B4=EB=A9=94=EC=9D=BC=20?= =?UTF-8?q?=EC=86=A1=EC=88=98=EC=8B=A0=ED=95=98=EB=8A=94=20=EC=84=9C?= =?UTF-8?q?=EB=B9=84=EC=8A=A4=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shop/global/util/EmailSenderTest.java | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 src/test/java/com/example/shop/global/util/EmailSenderTest.java diff --git a/src/test/java/com/example/shop/global/util/EmailSenderTest.java b/src/test/java/com/example/shop/global/util/EmailSenderTest.java new file mode 100644 index 0000000..551be27 --- /dev/null +++ b/src/test/java/com/example/shop/global/util/EmailSenderTest.java @@ -0,0 +1,114 @@ +package com.example.shop.global.util; + +import com.example.shop.admin.dto.OrderDeliveryRequest; +import com.example.shop.global.config.RedisTestConfig; +import com.icegreen.greenmail.configuration.GreenMailConfiguration; +import com.icegreen.greenmail.junit5.GreenMailExtension; +import com.icegreen.greenmail.util.GreenMailUtil; +import com.icegreen.greenmail.util.ServerSetupTest; +import jakarta.mail.MessagingException; +import jakarta.mail.internet.MimeMessage; +import org.apache.commons.codec.DecoderException; +import org.apache.commons.codec.net.QuotedPrintableCodec; +import org.assertj.core.api.SoftAssertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.ActiveProfiles; +import org.thymeleaf.context.Context; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +import static com.example.shop.global.util.TemplateEnginUtil.templateEngine; + +@SpringBootTest +@ActiveProfiles("test") +@Import(RedisTestConfig.class) +class EmailSenderTest { + + @RegisterExtension + static GreenMailExtension greenMail = new GreenMailExtension(ServerSetupTest.SMTP) + .withConfiguration(GreenMailConfiguration.aConfig().withUser("testMail", "testMail")) + .withPerMethodLifecycle(true); // 테스트 메서드마다 새로운 GreenMail 인스턴스 사용 + + @Autowired + private EmailSender emailSender; + + @DisplayName("이메일 인증 코드 송수신 테스트") + @Test + void sendAuthCode() { + // given + String receivedEmail = "receivedmail@test.com"; + String subject = "test subject"; + String content = authCodeTemplate("test authCode"); + + // when + emailSender.sendMail(receivedEmail, subject, content); + greenMail.waitForIncomingEmail(1); // 비동기로 동작하기 때문에 대기함 + + //then + verifyReceivedEmail(receivedEmail, subject, content); + } + + @DisplayName("주문 배송 알림 이메일 송수신 테스트") + @Test + void sendDeliveryAlertEmail() { + // given + OrderDeliveryRequest order = new OrderDeliveryRequest("tester@test.com", "Test Order Number"); + String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy년 MM월 dd일")); + String receivedEmail = "tester@test.com"; + String subject = "test subject"; + String content = orderDeliveryTemplate(order); + + // when + emailSender.sendMail(receivedEmail, subject, content); + greenMail.waitForIncomingEmail(1); // 비동기로 동작하기 때문에 대기함 + + //then + verifyReceivedEmail(receivedEmail, subject, content); + } + + private void verifyReceivedEmail(String receivedEmail, String subject, String content) { + QuotedPrintableCodec codec = new QuotedPrintableCodec("UTF-8"); // 한글을 디코딩하기 위함 + MimeMessage[] receivedMessages = greenMail.getReceivedMessages(); + + SoftAssertions.assertSoftly(softAssertions -> { + try { + softAssertions.assertThat(receivedMessages[0].getAllRecipients()[0].toString()) + .isEqualTo(receivedEmail); + + softAssertions.assertThat(receivedMessages[0].getSubject()) + .isEqualTo(subject); + + softAssertions.assertThat(codec.decode(GreenMailUtil.getBody(receivedMessages[0]))) + .isEqualToIgnoringNewLines(content); + + softAssertions.assertThatThrownBy(() -> GreenMailUtil.getBody(receivedMessages[1])) + .isExactlyInstanceOf(ArrayIndexOutOfBoundsException.class); + + } catch (MessagingException | DecoderException e) { + e.printStackTrace(); + } + }); + } + + private String authCodeTemplate(String authCode) { + Context context = new Context(); + context.setVariable("authCode", authCode); + return templateEngine.process("auth-code-email", context); + } + + private String orderDeliveryTemplate(OrderDeliveryRequest order) { + String orderNumber = order.getOrderNumber(); + String deliveryDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy년 MM월 dd일")); + + Context context = new Context(); + context.setVariable("orderNumber", orderNumber); + context.setVariable("deliveryDate", deliveryDate); + return templateEngine.process("delivery-email", context); + } +} \ No newline at end of file From 260d4fb0dc8a21085ac9aa2008ea47421fe674d1 Mon Sep 17 00:00:00 2001 From: DongminL Date: Sat, 7 Dec 2024 12:08:34 +0900 Subject: [PATCH 09/22] =?UTF-8?q?refactor:=20TestContainer=EB=A5=BC=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=ED=95=98=EC=97=AC=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/shop/admin/dao/OrderDeliveryRepositoryTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/example/shop/admin/dao/OrderDeliveryRepositoryTest.java b/src/test/java/com/example/shop/admin/dao/OrderDeliveryRepositoryTest.java index 6446489..2ae0432 100644 --- a/src/test/java/com/example/shop/admin/dao/OrderDeliveryRepositoryTest.java +++ b/src/test/java/com/example/shop/admin/dao/OrderDeliveryRepositoryTest.java @@ -1,6 +1,7 @@ package com.example.shop.admin.dao; import com.example.shop.admin.dto.OrderDeliveryRequest; +import com.example.shop.global.annotation.RedisTest; import org.assertj.core.api.SoftAssertions; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -14,8 +15,9 @@ import static com.example.shop.batch.util.OrderDeliveryBatchUtil.getOrderKeyYesterday; import static org.assertj.core.api.Assertions.assertThat; + @SpringBootTest -class OrderDeliveryRepositoryTest { +class OrderDeliveryRepositoryTest extends RedisTest { @Autowired private OrderDeliveryRepository orderDeliveryRepository; From 8c7e22ba2b653e00cc95c4b4c00223fd1f523e52 Mon Sep 17 00:00:00 2001 From: DongminL Date: Sat, 7 Dec 2024 23:57:01 +0900 Subject: [PATCH 10/22] =?UTF-8?q?feat:=20h2=20DB=EB=A5=BC=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/shop/global/annotation/RepositoryTest.java | 2 ++ .../java/com/example/shop/global/annotation/ServiceTest.java | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/test/java/com/example/shop/global/annotation/RepositoryTest.java b/src/test/java/com/example/shop/global/annotation/RepositoryTest.java index b0e6141..a02f551 100644 --- a/src/test/java/com/example/shop/global/annotation/RepositoryTest.java +++ b/src/test/java/com/example/shop/global/annotation/RepositoryTest.java @@ -1,9 +1,11 @@ package com.example.shop.global.annotation; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.test.context.ActiveProfiles; @DataJpaTest @ActiveProfiles("test") +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.ANY) public abstract class RepositoryTest { } diff --git a/src/test/java/com/example/shop/global/annotation/ServiceTest.java b/src/test/java/com/example/shop/global/annotation/ServiceTest.java index c0adbad..7442402 100644 --- a/src/test/java/com/example/shop/global/annotation/ServiceTest.java +++ b/src/test/java/com/example/shop/global/annotation/ServiceTest.java @@ -2,6 +2,7 @@ import com.example.shop.global.config.RedisTestConfig; import com.example.shop.global.config.RedisTestContainer; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Import; import org.springframework.test.context.ActiveProfiles; @@ -9,5 +10,6 @@ @SpringBootTest @ActiveProfiles("test") @Import(RedisTestConfig.class) +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.ANY) public abstract class ServiceTest extends RedisTestContainer { } From 681ff87e7805e79b851017e3aee436fef2c832f1 Mon Sep 17 00:00:00 2001 From: DongminL Date: Sun, 8 Dec 2024 00:00:33 +0900 Subject: [PATCH 11/22] =?UTF-8?q?refactor:=20h2=20DB=EB=A5=BC=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EA=B3=A0,=20.sql=20=ED=8C=8C=EC=9D=BC?= =?UTF-8?q?=EB=A1=9C=20=EB=8D=94=EB=AF=B8=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=ED=95=98=EC=97=AC=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../batch/OrderUpdateSenderWriterTest.java | 37 ++++++++----------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/src/test/java/com/example/shop/batch/OrderUpdateSenderWriterTest.java b/src/test/java/com/example/shop/batch/OrderUpdateSenderWriterTest.java index 1273375..1f82429 100644 --- a/src/test/java/com/example/shop/batch/OrderUpdateSenderWriterTest.java +++ b/src/test/java/com/example/shop/batch/OrderUpdateSenderWriterTest.java @@ -7,9 +7,8 @@ import com.example.shop.domain.order.Order; import com.example.shop.domain.order.OrderRepository; import com.example.shop.domain.order.OrderStatus; -import com.example.shop.domain.user.Role; -import com.example.shop.domain.user.User; import com.example.shop.domain.user.UserRepository; +import com.example.shop.global.annotation.ServiceTest; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; @@ -17,20 +16,24 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.transaction.annotation.Transactional; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.jdbc.Sql; -import java.util.ArrayList; import java.util.List; import static com.example.shop.batch.util.OrderDeliveryBatchUtil.getOrderKeyYesterday; +import static org.mockito.Mockito.doNothing; -@Transactional -@SpringBootTest -class OrderUpdateSenderWriterTest { - @Autowired +@ExtendWith(MockitoExtension.class) +@Sql(scripts = "classpath:/sql/order-data.sql", + executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) +class OrderUpdateSenderWriterTest extends ServiceTest { + + @MockBean private AdminOrderService adminOrderService; @Autowired private OrderDeliveryRepository orderDeliveryRepository; @@ -43,19 +46,9 @@ class OrderUpdateSenderWriterTest { @BeforeEach void setup() { - User user = User.builder() - .email("test@test.com") - .password("test") - .userRole(Role.ROLE_USER) - .orders(new ArrayList<>()) - .userName("tester") - .build(); - - user = userRepository.save(user); - - for (long i = 1; i <= 532; i++) { + for (long i = 1; i <= 5; i++) { orderDeliveryRepository.addOrderEmail(getOrderKeyYesterday(), - new OrderDeliveryRequest(user.getEmail(), i+"test")); + new OrderDeliveryRequest("test@test.com", i+"test")); } } @@ -70,7 +63,7 @@ void write() { sqlSession.flushStatements(); } finally { - orderDeliveryRequestList.forEach(adminOrderService::sendDeliveryAlertEmail); + orderDeliveryRequestList.forEach(doNothing().when(adminOrderService)::sendDeliveryAlertEmail); orderDeliveryRepository.removeOrderEmail(getOrderKeyYesterday(), orderDeliveryRequestList.toArray()); } From c9510b0164d45e239ae85da8d17472d9c2866f46 Mon Sep 17 00:00:00 2001 From: DongminL Date: Sun, 8 Dec 2024 00:02:19 +0900 Subject: [PATCH 12/22] =?UTF-8?q?refactor:=20h2=20DB=EB=A5=BC=20=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EA=B3=A0,=20.sql=20=ED=8C=8C=EC=9D=BC?= =?UTF-8?q?=EB=A1=9C=20=EB=8D=94=EB=AF=B8=20=EB=8D=B0=EC=9D=B4=ED=84=B0=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=ED=95=98=EC=97=AC=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../batch/OrderUpdateSenderWriterTest.java | 3 --- src/test/resources/sql/order-data.sql | 22 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 src/test/resources/sql/order-data.sql diff --git a/src/test/java/com/example/shop/batch/OrderUpdateSenderWriterTest.java b/src/test/java/com/example/shop/batch/OrderUpdateSenderWriterTest.java index 1f82429..1b5db49 100644 --- a/src/test/java/com/example/shop/batch/OrderUpdateSenderWriterTest.java +++ b/src/test/java/com/example/shop/batch/OrderUpdateSenderWriterTest.java @@ -7,7 +7,6 @@ import com.example.shop.domain.order.Order; import com.example.shop.domain.order.OrderRepository; import com.example.shop.domain.order.OrderStatus; -import com.example.shop.domain.user.UserRepository; import com.example.shop.global.annotation.ServiceTest; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; @@ -41,8 +40,6 @@ class OrderUpdateSenderWriterTest extends ServiceTest { private SqlSessionFactory sqlSessionFactory; @Autowired private OrderRepository orderRepository; - @Autowired - private UserRepository userRepository; @BeforeEach void setup() { diff --git a/src/test/resources/sql/order-data.sql b/src/test/resources/sql/order-data.sql new file mode 100644 index 0000000..b6e5b41 --- /dev/null +++ b/src/test/resources/sql/order-data.sql @@ -0,0 +1,22 @@ +-- User 테이블에 데이터 삽입 +INSERT INTO users (email, password, user_role, user_name, created_at, updated_at) +VALUES + ('test@test.com', 'test', 'ROLE_USER', 'tester', now(), now()); + +-- Order 테이블에 데이터 삽입 +INSERT INTO orders ( + created_at, updated_at, receiver_address, receiver_name, receiver_phone, + receiver_postal_code, shipping_message, order_number, order_status, + total_price, user_id +) +VALUES + (CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'Address 1', 'User 1', '0100000001', + '10001', 'Test message', '1test', 'PAID', 10500.00, 1), + (CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'Address 2', 'User 2', '0100000002', + '10002', 'Test message', '2test', 'PAID', 11000.00, 1), + (CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'Address 3', 'User 3', '0100000003', + '10003', 'Test message', '3test', 'PAID', 11500.00, 1), + (CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'Address 4', 'User 4', '0100000004', + '10004', 'Test message', '4test', 'PAID', 12000.00, 1), + (CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 'Address 5', 'User 5', '0100000005', + '10005', 'Test message', '5test', 'PAID', 12500.00, 1); From 5d7d67a7d07c032007c5e64a7c5351ff0710c9ac Mon Sep 17 00:00:00 2001 From: DongminL Date: Sun, 8 Dec 2024 00:17:42 +0900 Subject: [PATCH 13/22] =?UTF-8?q?test:=20test=EC=9A=A9=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95=20=ED=8C=8C=EC=9D=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/resources/application-test.yaml | 68 ++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/test/resources/application-test.yaml diff --git a/src/test/resources/application-test.yaml b/src/test/resources/application-test.yaml new file mode 100644 index 0000000..b20b982 --- /dev/null +++ b/src/test/resources/application-test.yaml @@ -0,0 +1,68 @@ +spring: + datasource: + driver-class-name: org.h2.Driver + url: jdbc:h2:mem/shop;MODE=MYSQL;DB_CLOSE_DELAY=-1;DATABASE_TO_LOWER=TRUE + username: sa + password: + + + jpa: + show-sql: true + hibernate: + ddl-auto: create-drop + properties: + hibernate: + format_sql: true + dialect: org.hibernate.dialect.H2Dialect + defer-datasource-initialization: true + + mail: + host: 127.0.0.1 + port: 3025 + username: testMail + password: testMail + properties: + mail: + smtp: + auth: true + starttls: + enable: true + transport: + protocol: smtp + debug: true + default-encoding: UTF-8 + + data: + redis: + port: 6379 + host: localhost + + batch: + jdbc: + initialize-schema: always + + web: + resources: + static-locations: file:///C:/ProductImage/ # 이미지 저장 경로 설정 + +server: + shutdown: graceful + +jwt: + secret: dgedgbdafmjgbaasfgadbsgmadfhgbfamfdsbvmssdgsdfgdf + + +mybatis: + mapper-locations: classpath:mappers/*.xml + type-aliases-package: com.example.shop.user.dto, com.example.shop.admin.dto + configuration: + map-underscore-to-camel-case: true + log-impl: org.apache.ibatis.logging.stdout.StdOutImpl + +logging: + level: + root: INFO + org.mybatis: DEBUG + org.apache.ibatis: DEBUG + org.mybatis.spring.SqlSessionFactoryBean: DEBUG + org.springframework.jdbc.datasource.DataSourceTransactionManager: DEBUG From 0e817207675e2883f3d616db1926e7e345490dff Mon Sep 17 00:00:00 2001 From: DongminL Date: Sun, 8 Dec 2024 00:19:31 +0900 Subject: [PATCH 14/22] =?UTF-8?q?style:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=B4=88=EA=B8=B0=20=EC=84=A4=EC=A0=95=20=ED=8C=A8=ED=82=A4?= =?UTF-8?q?=EC=A7=80=EB=AA=85=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/shop/admin/dao/OrderDeliveryRepositoryTest.java | 2 +- .../com/example/shop/batch/OrderUpdateSenderWriterTest.java | 2 +- .../shop/global/{annotation => setting}/ControllerTest.java | 2 +- .../example/shop/global/{annotation => setting}/RedisTest.java | 2 +- .../shop/global/{annotation => setting}/RepositoryTest.java | 2 +- .../shop/global/{annotation => setting}/ServiceTest.java | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) rename src/test/java/com/example/shop/global/{annotation => setting}/ControllerTest.java (97%) rename src/test/java/com/example/shop/global/{annotation => setting}/RedisTest.java (90%) rename src/test/java/com/example/shop/global/{annotation => setting}/RepositoryTest.java (89%) rename src/test/java/com/example/shop/global/{annotation => setting}/ServiceTest.java (92%) diff --git a/src/test/java/com/example/shop/admin/dao/OrderDeliveryRepositoryTest.java b/src/test/java/com/example/shop/admin/dao/OrderDeliveryRepositoryTest.java index 2ae0432..f361c14 100644 --- a/src/test/java/com/example/shop/admin/dao/OrderDeliveryRepositoryTest.java +++ b/src/test/java/com/example/shop/admin/dao/OrderDeliveryRepositoryTest.java @@ -1,7 +1,7 @@ package com.example.shop.admin.dao; import com.example.shop.admin.dto.OrderDeliveryRequest; -import com.example.shop.global.annotation.RedisTest; +import com.example.shop.global.setting.RedisTest; import org.assertj.core.api.SoftAssertions; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; diff --git a/src/test/java/com/example/shop/batch/OrderUpdateSenderWriterTest.java b/src/test/java/com/example/shop/batch/OrderUpdateSenderWriterTest.java index 1b5db49..9ef52bf 100644 --- a/src/test/java/com/example/shop/batch/OrderUpdateSenderWriterTest.java +++ b/src/test/java/com/example/shop/batch/OrderUpdateSenderWriterTest.java @@ -7,7 +7,7 @@ import com.example.shop.domain.order.Order; import com.example.shop.domain.order.OrderRepository; import com.example.shop.domain.order.OrderStatus; -import com.example.shop.global.annotation.ServiceTest; +import com.example.shop.global.setting.ServiceTest; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; diff --git a/src/test/java/com/example/shop/global/annotation/ControllerTest.java b/src/test/java/com/example/shop/global/setting/ControllerTest.java similarity index 97% rename from src/test/java/com/example/shop/global/annotation/ControllerTest.java rename to src/test/java/com/example/shop/global/setting/ControllerTest.java index f2302b9..8f81ee3 100644 --- a/src/test/java/com/example/shop/global/annotation/ControllerTest.java +++ b/src/test/java/com/example/shop/global/setting/ControllerTest.java @@ -1,4 +1,4 @@ -package com.example.shop.global.annotation; +package com.example.shop.global.setting; import com.example.shop.admin.controller.AdminOrderController; import com.example.shop.admin.controller.AdminProductController; diff --git a/src/test/java/com/example/shop/global/annotation/RedisTest.java b/src/test/java/com/example/shop/global/setting/RedisTest.java similarity index 90% rename from src/test/java/com/example/shop/global/annotation/RedisTest.java rename to src/test/java/com/example/shop/global/setting/RedisTest.java index fbca1fe..a240d2d 100644 --- a/src/test/java/com/example/shop/global/annotation/RedisTest.java +++ b/src/test/java/com/example/shop/global/setting/RedisTest.java @@ -1,4 +1,4 @@ -package com.example.shop.global.annotation; +package com.example.shop.global.setting; import com.example.shop.global.config.RedisTestConfig; import com.example.shop.global.config.RedisTestContainer; diff --git a/src/test/java/com/example/shop/global/annotation/RepositoryTest.java b/src/test/java/com/example/shop/global/setting/RepositoryTest.java similarity index 89% rename from src/test/java/com/example/shop/global/annotation/RepositoryTest.java rename to src/test/java/com/example/shop/global/setting/RepositoryTest.java index a02f551..47011f6 100644 --- a/src/test/java/com/example/shop/global/annotation/RepositoryTest.java +++ b/src/test/java/com/example/shop/global/setting/RepositoryTest.java @@ -1,4 +1,4 @@ -package com.example.shop.global.annotation; +package com.example.shop.global.setting; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; diff --git a/src/test/java/com/example/shop/global/annotation/ServiceTest.java b/src/test/java/com/example/shop/global/setting/ServiceTest.java similarity index 92% rename from src/test/java/com/example/shop/global/annotation/ServiceTest.java rename to src/test/java/com/example/shop/global/setting/ServiceTest.java index 7442402..a1416a5 100644 --- a/src/test/java/com/example/shop/global/annotation/ServiceTest.java +++ b/src/test/java/com/example/shop/global/setting/ServiceTest.java @@ -1,4 +1,4 @@ -package com.example.shop.global.annotation; +package com.example.shop.global.setting; import com.example.shop.global.config.RedisTestConfig; import com.example.shop.global.config.RedisTestContainer; From 177332088de45e76f2f4d8430c2acdbfb8c61b35 Mon Sep 17 00:00:00 2001 From: DongminL Date: Sun, 8 Dec 2024 01:53:24 +0900 Subject: [PATCH 15/22] =?UTF-8?q?test:=20CI=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/pr-ci.yaml | 59 ++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 .github/workflows/pr-ci.yaml diff --git a/.github/workflows/pr-ci.yaml b/.github/workflows/pr-ci.yaml new file mode 100644 index 0000000..89c64b5 --- /dev/null +++ b/.github/workflows/pr-ci.yaml @@ -0,0 +1,59 @@ +name: PR 시 CI 테스트 자동화 + +on: + pull_request: + branches: + - main + +jobs: + test: + name: Test + runs-on: ubuntu-latest + + permissions: + checks: write + pull-requests: write + + steps: + - name: 레포지토리 체크아웃 + uses: actions/checkout@v4 + with: + token: ${{ secrets.GIT_TOKEN }} + - run: touch ./src/main/resources/security.properties + - run: echo "${{ secrets.SECURITY_PROP }}" > ./src/main/resources/security.properties + + - name: JDK 17 설치 + uses: actions/setup-java@v4 + with: + distribution: 'oracle' + java-version: '17' + cache: 'gradle' + + - name: Gradle 종속성 캐시 + uses: actions/cache@v4 + with: + path: | + ~/.gradle/caches + ~/.gradle/wrapper + key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} + restore-keys: | + ${{ runner.os }}-gradle- + + - name: Gradle 명령 실행 권한 부여 + run: chmod +x ./gradlew + shell: bash + + - name: Gradle로 프로젝트 Build + run: ./gradlew build + + - name: 테스트 결과를 PR 코멘트로 출력 + uses: EnricoMi/publish-unit-test-result-action@v2 + if: always() + with: + files: '**/build/test-results/test/TEST-*.xml' + + - name: 테스트 실패 시, 오류가 발생한 코드 라인에 코멘트 추가 + uses: mikepenz/action-junit-report@v4 + if: always() + with: + report_paths: '**/build/test-results/test/TEST-*.xml' \ No newline at end of file From 8f7af071a277578b1ad7cba49fabeae9e38af0bb Mon Sep 17 00:00:00 2001 From: DongminL Date: Sun, 8 Dec 2024 02:14:47 +0900 Subject: [PATCH 16/22] =?UTF-8?q?test:=20test=20=ED=99=98=EA=B2=BD?= =?UTF-8?q?=EC=97=90=EC=84=9C=20spring=20batch=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/resources/application-test.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/resources/application-test.yaml b/src/test/resources/application-test.yaml index b20b982..0871b4e 100644 --- a/src/test/resources/application-test.yaml +++ b/src/test/resources/application-test.yaml @@ -40,6 +40,7 @@ spring: batch: jdbc: initialize-schema: always + schema: classpath:org/springframework/batch/core/schema-h2.sql web: resources: From 8429107524dda6fdc3d9465d7136826a3bef65e5 Mon Sep 17 00:00:00 2001 From: DongminL Date: Sun, 8 Dec 2024 02:22:52 +0900 Subject: [PATCH 17/22] =?UTF-8?q?test:=20test=20=ED=99=98=EA=B2=BD?= =?UTF-8?q?=EC=97=90=EC=84=9C=20spring=20batch=EC=9D=98=20DB=EB=A5=BC=20H2?= =?UTF-8?q?=EB=A1=9C=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shop/global/config/BatchTestConfig.java | 21 +++++++++++++++++++ .../shop/global/setting/RedisTest.java | 3 ++- .../shop/global/setting/ServiceTest.java | 3 ++- 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 src/test/java/com/example/shop/global/config/BatchTestConfig.java diff --git a/src/test/java/com/example/shop/global/config/BatchTestConfig.java b/src/test/java/com/example/shop/global/config/BatchTestConfig.java new file mode 100644 index 0000000..7b05bd4 --- /dev/null +++ b/src/test/java/com/example/shop/global/config/BatchTestConfig.java @@ -0,0 +1,21 @@ +package com.example.shop.global.config; + +import org.springframework.boot.jdbc.DataSourceBuilder; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; + +import javax.sql.DataSource; + +@TestConfiguration +public class BatchTestConfig { + + @Bean + public DataSource dataSource() { + return DataSourceBuilder.create() + .driverClassName("org.h2.Driver") + .url("jdbc:h2:mem:testdb;MODE=MySQL") + .username("sa") + .password("") + .build(); + } +} diff --git a/src/test/java/com/example/shop/global/setting/RedisTest.java b/src/test/java/com/example/shop/global/setting/RedisTest.java index a240d2d..990e584 100644 --- a/src/test/java/com/example/shop/global/setting/RedisTest.java +++ b/src/test/java/com/example/shop/global/setting/RedisTest.java @@ -1,5 +1,6 @@ package com.example.shop.global.setting; +import com.example.shop.global.config.BatchTestConfig; import com.example.shop.global.config.RedisTestConfig; import com.example.shop.global.config.RedisTestContainer; import org.springframework.boot.test.context.SpringBootTest; @@ -8,6 +9,6 @@ @SpringBootTest @ActiveProfiles("test") -@Import(RedisTestConfig.class) +@Import({RedisTestConfig.class, BatchTestConfig.class}) public abstract class RedisTest extends RedisTestContainer { } diff --git a/src/test/java/com/example/shop/global/setting/ServiceTest.java b/src/test/java/com/example/shop/global/setting/ServiceTest.java index a1416a5..f49604e 100644 --- a/src/test/java/com/example/shop/global/setting/ServiceTest.java +++ b/src/test/java/com/example/shop/global/setting/ServiceTest.java @@ -1,5 +1,6 @@ package com.example.shop.global.setting; +import com.example.shop.global.config.BatchTestConfig; import com.example.shop.global.config.RedisTestConfig; import com.example.shop.global.config.RedisTestContainer; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; @@ -9,7 +10,7 @@ @SpringBootTest @ActiveProfiles("test") -@Import(RedisTestConfig.class) +@Import({RedisTestConfig.class, BatchTestConfig.class}) @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.ANY) public abstract class ServiceTest extends RedisTestContainer { } From d055f26808c1625bd3fbfbd68a98b38e13ac2fa8 Mon Sep 17 00:00:00 2001 From: DongminL Date: Sun, 8 Dec 2024 02:28:34 +0900 Subject: [PATCH 18/22] =?UTF-8?q?fix:=20spring=20batch=EA=B0=80=20h2=20db?= =?UTF-8?q?=EC=97=90=20=EC=A0=81=EC=9A=A9=EB=90=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shop/global/config/BatchTestConfig.java | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/test/java/com/example/shop/global/config/BatchTestConfig.java b/src/test/java/com/example/shop/global/config/BatchTestConfig.java index 7b05bd4..fb57dd3 100644 --- a/src/test/java/com/example/shop/global/config/BatchTestConfig.java +++ b/src/test/java/com/example/shop/global/config/BatchTestConfig.java @@ -1,21 +1,11 @@ package com.example.shop.global.config; -import org.springframework.boot.jdbc.DataSourceBuilder; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.TestConfiguration; -import org.springframework.context.annotation.Bean; - -import javax.sql.DataSource; @TestConfiguration +@EnableBatchProcessing +@EnableAutoConfiguration public class BatchTestConfig { - - @Bean - public DataSource dataSource() { - return DataSourceBuilder.create() - .driverClassName("org.h2.Driver") - .url("jdbc:h2:mem:testdb;MODE=MySQL") - .username("sa") - .password("") - .build(); - } } From b0c4567cd15c01c55de744d08891a88f6607ce1a Mon Sep 17 00:00:00 2001 From: DongminL Date: Sun, 8 Dec 2024 02:31:08 +0900 Subject: [PATCH 19/22] =?UTF-8?q?fix:=20spring=20batch=EA=B0=80=20h2=20db?= =?UTF-8?q?=EC=97=90=20=EC=A0=81=EC=9A=A9=EB=90=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=84=A4=EC=A0=952?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/shop/global/config/BatchTestConfig.java | 4 ++-- src/test/resources/application-test.yaml | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/example/shop/global/config/BatchTestConfig.java b/src/test/java/com/example/shop/global/config/BatchTestConfig.java index fb57dd3..34411c5 100644 --- a/src/test/java/com/example/shop/global/config/BatchTestConfig.java +++ b/src/test/java/com/example/shop/global/config/BatchTestConfig.java @@ -2,9 +2,9 @@ import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Configuration; -@TestConfiguration +@Configuration @EnableBatchProcessing @EnableAutoConfiguration public class BatchTestConfig { diff --git a/src/test/resources/application-test.yaml b/src/test/resources/application-test.yaml index 0871b4e..c6c0337 100644 --- a/src/test/resources/application-test.yaml +++ b/src/test/resources/application-test.yaml @@ -5,7 +5,6 @@ spring: username: sa password: - jpa: show-sql: true hibernate: @@ -15,6 +14,7 @@ spring: format_sql: true dialect: org.hibernate.dialect.H2Dialect defer-datasource-initialization: true + database: h2 mail: host: 127.0.0.1 @@ -40,7 +40,6 @@ spring: batch: jdbc: initialize-schema: always - schema: classpath:org/springframework/batch/core/schema-h2.sql web: resources: From 9452b6d73d06692aab6d0e89847f176628ee0355 Mon Sep 17 00:00:00 2001 From: DongminL Date: Sun, 8 Dec 2024 02:46:13 +0900 Subject: [PATCH 20/22] =?UTF-8?q?fix:=20spring=20batch=EA=B0=80=20h2=20db?= =?UTF-8?q?=EC=97=90=20=EC=A0=81=EC=9A=A9=EB=90=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=84=A4=EC=A0=953?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 2 +- .../example/shop/global/config/BatchTestConfig.java | 11 ----------- .../com/example/shop/global/setting/RedisTest.java | 3 +-- .../com/example/shop/global/setting/ServiceTest.java | 3 +-- 4 files changed, 3 insertions(+), 16 deletions(-) delete mode 100644 src/test/java/com/example/shop/global/config/BatchTestConfig.java diff --git a/build.gradle b/build.gradle index 7fbace4..656f936 100644 --- a/build.gradle +++ b/build.gradle @@ -44,7 +44,7 @@ dependencies { // DB implementation 'org.mariadb.jdbc:mariadb-java-client:3.1.2' - testRuntimeOnly 'com.h2database:h2' + runtimeOnly 'com.h2database:h2' // Redis implementation 'org.springframework.boot:spring-boot-starter-data-redis' diff --git a/src/test/java/com/example/shop/global/config/BatchTestConfig.java b/src/test/java/com/example/shop/global/config/BatchTestConfig.java deleted file mode 100644 index 34411c5..0000000 --- a/src/test/java/com/example/shop/global/config/BatchTestConfig.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.example.shop.global.config; - -import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.context.annotation.Configuration; - -@Configuration -@EnableBatchProcessing -@EnableAutoConfiguration -public class BatchTestConfig { -} diff --git a/src/test/java/com/example/shop/global/setting/RedisTest.java b/src/test/java/com/example/shop/global/setting/RedisTest.java index 990e584..a240d2d 100644 --- a/src/test/java/com/example/shop/global/setting/RedisTest.java +++ b/src/test/java/com/example/shop/global/setting/RedisTest.java @@ -1,6 +1,5 @@ package com.example.shop.global.setting; -import com.example.shop.global.config.BatchTestConfig; import com.example.shop.global.config.RedisTestConfig; import com.example.shop.global.config.RedisTestContainer; import org.springframework.boot.test.context.SpringBootTest; @@ -9,6 +8,6 @@ @SpringBootTest @ActiveProfiles("test") -@Import({RedisTestConfig.class, BatchTestConfig.class}) +@Import(RedisTestConfig.class) public abstract class RedisTest extends RedisTestContainer { } diff --git a/src/test/java/com/example/shop/global/setting/ServiceTest.java b/src/test/java/com/example/shop/global/setting/ServiceTest.java index f49604e..a1416a5 100644 --- a/src/test/java/com/example/shop/global/setting/ServiceTest.java +++ b/src/test/java/com/example/shop/global/setting/ServiceTest.java @@ -1,6 +1,5 @@ package com.example.shop.global.setting; -import com.example.shop.global.config.BatchTestConfig; import com.example.shop.global.config.RedisTestConfig; import com.example.shop.global.config.RedisTestContainer; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; @@ -10,7 +9,7 @@ @SpringBootTest @ActiveProfiles("test") -@Import({RedisTestConfig.class, BatchTestConfig.class}) +@Import(RedisTestConfig.class) @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.ANY) public abstract class ServiceTest extends RedisTestContainer { } From fde150cb67555513016fd396b4825361156c344d Mon Sep 17 00:00:00 2001 From: DongminL Date: Sun, 8 Dec 2024 03:17:39 +0900 Subject: [PATCH 21/22] =?UTF-8?q?fix:=20spring=20batch=EA=B0=80=20h2=20db?= =?UTF-8?q?=EC=97=90=20=EC=A0=81=EC=9A=A9=EB=90=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=84=A4=EC=A0=954?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/shop/global/config/BatchTestConfig.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/test/java/com/example/shop/global/config/BatchTestConfig.java diff --git a/src/test/java/com/example/shop/global/config/BatchTestConfig.java b/src/test/java/com/example/shop/global/config/BatchTestConfig.java new file mode 100644 index 0000000..96fd677 --- /dev/null +++ b/src/test/java/com/example/shop/global/config/BatchTestConfig.java @@ -0,0 +1,9 @@ +package com.example.shop.global.config; + +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableBatchProcessing +public class BatchTestConfig { +} From 38fc6b6773d3ea483f2ddf68447ed51c73dd5bb7 Mon Sep 17 00:00:00 2001 From: DongminL Date: Sun, 8 Dec 2024 03:26:53 +0900 Subject: [PATCH 22/22] =?UTF-8?q?fix:=20=EC=8B=A4=ED=8C=A8=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=ED=85=8C=EC=8A=A4=ED=8A=B8=EC=97=90=20@AutoConfigu?= =?UTF-8?q?reTestDatabase=20=EB=AA=85=EC=8B=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/com/example/shop/ShopApplicationTests.java | 2 ++ .../shop/admin/dao/OrderDeliveryRepositoryTest.java | 2 ++ .../com/example/shop/global/config/BatchTestConfig.java | 9 --------- .../com/example/shop/global/util/EmailSenderTest.java | 2 ++ 4 files changed, 6 insertions(+), 9 deletions(-) delete mode 100644 src/test/java/com/example/shop/global/config/BatchTestConfig.java diff --git a/src/test/java/com/example/shop/ShopApplicationTests.java b/src/test/java/com/example/shop/ShopApplicationTests.java index 47684f7..695059c 100644 --- a/src/test/java/com/example/shop/ShopApplicationTests.java +++ b/src/test/java/com/example/shop/ShopApplicationTests.java @@ -1,9 +1,11 @@ package com.example.shop; import org.junit.jupiter.api.Test; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.ANY) class ShopApplicationTests { @Test diff --git a/src/test/java/com/example/shop/admin/dao/OrderDeliveryRepositoryTest.java b/src/test/java/com/example/shop/admin/dao/OrderDeliveryRepositoryTest.java index f361c14..e4e36d2 100644 --- a/src/test/java/com/example/shop/admin/dao/OrderDeliveryRepositoryTest.java +++ b/src/test/java/com/example/shop/admin/dao/OrderDeliveryRepositoryTest.java @@ -8,6 +8,7 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.context.SpringBootTest; import java.util.Set; @@ -17,6 +18,7 @@ @SpringBootTest +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.ANY) class OrderDeliveryRepositoryTest extends RedisTest { @Autowired diff --git a/src/test/java/com/example/shop/global/config/BatchTestConfig.java b/src/test/java/com/example/shop/global/config/BatchTestConfig.java deleted file mode 100644 index 96fd677..0000000 --- a/src/test/java/com/example/shop/global/config/BatchTestConfig.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.example.shop.global.config; - -import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; -import org.springframework.context.annotation.Configuration; - -@Configuration -@EnableBatchProcessing -public class BatchTestConfig { -} diff --git a/src/test/java/com/example/shop/global/util/EmailSenderTest.java b/src/test/java/com/example/shop/global/util/EmailSenderTest.java index 551be27..bae23d5 100644 --- a/src/test/java/com/example/shop/global/util/EmailSenderTest.java +++ b/src/test/java/com/example/shop/global/util/EmailSenderTest.java @@ -15,6 +15,7 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.Import; import org.springframework.test.context.ActiveProfiles; @@ -28,6 +29,7 @@ @SpringBootTest @ActiveProfiles("test") @Import(RedisTestConfig.class) +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.ANY) class EmailSenderTest { @RegisterExtension