From b86f6e40c93b95c4022acc2724ba5422849aff33 Mon Sep 17 00:00:00 2001 From: EunChanNam Date: Tue, 21 Nov 2023 21:28:31 +0900 Subject: [PATCH] =?UTF-8?q?enhancement:=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=ED=99=98=EA=B2=BD=20TestContainer=20=EB=A1=9C=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 --- build.gradle | 5 +++ .../wishhair/wesharewishhair/LikeTest.java | 6 +-- .../common/config/EmbeddedRedisConfig.java | 44 ------------------- .../common/support/ApiTestSupport.java | 2 +- .../common/support/TestContainerSupport.java | 33 ++++++++++++++ .../global/utils/RedisUtilsTest.java | 6 +-- .../LikeReviewControllerTest.java | 3 -- .../presentation/ReviewSearchApiTest.java | 3 -- src/test/resources/application-test.yml | 6 +-- 9 files changed, 45 insertions(+), 63 deletions(-) delete mode 100644 src/test/java/com/inq/wishhair/wesharewishhair/common/config/EmbeddedRedisConfig.java create mode 100644 src/test/java/com/inq/wishhair/wesharewishhair/common/support/TestContainerSupport.java diff --git a/build.gradle b/build.gradle index 3293ebc..43751b2 100644 --- a/build.gradle +++ b/build.gradle @@ -70,6 +70,11 @@ dependencies { implementation('it.ozimov:embedded-redis:0.7.2') implementation 'org.springframework.boot:spring-boot-starter-data-redis' + //테스트 + testImplementation "org.testcontainers:testcontainers:1.19.2" + testImplementation "org.testcontainers:junit-jupiter:1.19.2" + testImplementation "org.testcontainers:mysql:1.19.2" + //네이버 클라우드 implementation 'org.springframework.cloud:spring-cloud-starter-aws:2.2.6.RELEASE' diff --git a/src/test/java/com/inq/wishhair/wesharewishhair/LikeTest.java b/src/test/java/com/inq/wishhair/wesharewishhair/LikeTest.java index 1dfacf4..74a676f 100644 --- a/src/test/java/com/inq/wishhair/wesharewishhair/LikeTest.java +++ b/src/test/java/com/inq/wishhair/wesharewishhair/LikeTest.java @@ -11,15 +11,13 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.context.annotation.Import; -import com.inq.wishhair.wesharewishhair.common.config.EmbeddedRedisConfig; +import com.inq.wishhair.wesharewishhair.common.support.TestContainerSupport; import com.inq.wishhair.wesharewishhair.review.application.LikeReviewService; @SpringBootTest -@Import(EmbeddedRedisConfig.class) @DisplayName("[좋아요 동시성 테스트]") -class LikeTest { +class LikeTest extends TestContainerSupport { @Autowired private LikeReviewService likeReviewService; diff --git a/src/test/java/com/inq/wishhair/wesharewishhair/common/config/EmbeddedRedisConfig.java b/src/test/java/com/inq/wishhair/wesharewishhair/common/config/EmbeddedRedisConfig.java deleted file mode 100644 index 441c002..0000000 --- a/src/test/java/com/inq/wishhair/wesharewishhair/common/config/EmbeddedRedisConfig.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.inq.wishhair.wesharewishhair.common.config; - -import java.util.Set; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.test.context.TestConfiguration; -import org.springframework.data.redis.core.RedisTemplate; - -import jakarta.annotation.PostConstruct; -import jakarta.annotation.PreDestroy; -import redis.embedded.RedisServer; - -@TestConfiguration -public class EmbeddedRedisConfig { - - private final RedisServer redisServer; - private final RedisTemplate redisTemplate; - - public EmbeddedRedisConfig( - @Value("${spring.data.redis.port}") int port, - @Autowired RedisTemplate redisTemplate - ) { - this.redisServer = new RedisServer(port); - this.redisTemplate = redisTemplate; - } - - @PostConstruct - public void startRedis() { - try { - this.redisServer.start(); - } catch (RuntimeException e) { - Set keys = redisTemplate.keys("*"); - if (keys != null) { - redisTemplate.delete(keys); - } - } - } - - @PreDestroy - public void stopRedis() { - this.redisServer.stop(); - } -} \ No newline at end of file diff --git a/src/test/java/com/inq/wishhair/wesharewishhair/common/support/ApiTestSupport.java b/src/test/java/com/inq/wishhair/wesharewishhair/common/support/ApiTestSupport.java index a3f3492..43ef715 100644 --- a/src/test/java/com/inq/wishhair/wesharewishhair/common/support/ApiTestSupport.java +++ b/src/test/java/com/inq/wishhair/wesharewishhair/common/support/ApiTestSupport.java @@ -20,7 +20,7 @@ @SpringBootTest @AutoConfigureMockMvc @Transactional -public abstract class ApiTestSupport { +public abstract class ApiTestSupport extends TestContainerSupport { private final ObjectMapper objectMapper = new ObjectMapper(); diff --git a/src/test/java/com/inq/wishhair/wesharewishhair/common/support/TestContainerSupport.java b/src/test/java/com/inq/wishhair/wesharewishhair/common/support/TestContainerSupport.java new file mode 100644 index 0000000..c8a5953 --- /dev/null +++ b/src/test/java/com/inq/wishhair/wesharewishhair/common/support/TestContainerSupport.java @@ -0,0 +1,33 @@ +package com.inq.wishhair.wesharewishhair.common.support; + +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.JdbcDatabaseContainer; +import org.testcontainers.containers.MySQLContainer; +import org.testcontainers.utility.DockerImageName; +public abstract class TestContainerSupport { + + private static final String REDIS_IMAGE = "redis:latest"; + private static final int REDIS_PORT = 6379; + private static final String MYSQL_IMAGE = "mysql:8"; + + private static final GenericContainer REDIS; + private static final JdbcDatabaseContainer MYSQL; + + static { + REDIS = new GenericContainer(DockerImageName.parse(REDIS_IMAGE)) + .withExposedPorts(REDIS_PORT) + .withReuse(true); + MYSQL = new MySQLContainer(MYSQL_IMAGE); + + REDIS.start(); + MYSQL.start(); + } + + @DynamicPropertySource + public static void overrideProps(DynamicPropertyRegistry registry){ + registry.add("spring.data.redis.host", REDIS::getHost); + registry.add("spring.data.redis.port", () -> String.valueOf(REDIS.getMappedPort(REDIS_PORT))); + } +} diff --git a/src/test/java/com/inq/wishhair/wesharewishhair/global/utils/RedisUtilsTest.java b/src/test/java/com/inq/wishhair/wesharewishhair/global/utils/RedisUtilsTest.java index 2f44893..9c8862a 100644 --- a/src/test/java/com/inq/wishhair/wesharewishhair/global/utils/RedisUtilsTest.java +++ b/src/test/java/com/inq/wishhair/wesharewishhair/global/utils/RedisUtilsTest.java @@ -8,18 +8,16 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.context.annotation.Import; import org.springframework.data.redis.core.RedisTemplate; -import com.inq.wishhair.wesharewishhair.common.config.EmbeddedRedisConfig; +import com.inq.wishhair.wesharewishhair.common.support.TestContainerSupport; /** * 주의사항 : 각 테스트 케이스별로 저장하는 데이터를 다르게 해야함(레디스 서버는 복구가 안되기 때문) */ @SpringBootTest -@Import(EmbeddedRedisConfig.class) @DisplayName("[RedisUtils 테스트]") -class RedisUtilsTest { +class RedisUtilsTest extends TestContainerSupport { @Autowired private RedisUtils redisUtils; diff --git a/src/test/java/com/inq/wishhair/wesharewishhair/review/presentation/LikeReviewControllerTest.java b/src/test/java/com/inq/wishhair/wesharewishhair/review/presentation/LikeReviewControllerTest.java index 0242ec6..3f6f873 100644 --- a/src/test/java/com/inq/wishhair/wesharewishhair/review/presentation/LikeReviewControllerTest.java +++ b/src/test/java/com/inq/wishhair/wesharewishhair/review/presentation/LikeReviewControllerTest.java @@ -7,19 +7,16 @@ import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Import; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -import com.inq.wishhair.wesharewishhair.common.config.EmbeddedRedisConfig; import com.inq.wishhair.wesharewishhair.common.support.ApiTestSupport; import com.inq.wishhair.wesharewishhair.review.domain.likereview.LikeReview; import com.inq.wishhair.wesharewishhair.review.domain.likereview.LikeReviewRepository; import com.inq.wishhair.wesharewishhair.user.domain.entity.User; @DisplayName("[LikeReview API 테스트]") -@Import(EmbeddedRedisConfig.class) class LikeReviewControllerTest extends ApiTestSupport { @Autowired diff --git a/src/test/java/com/inq/wishhair/wesharewishhair/review/presentation/ReviewSearchApiTest.java b/src/test/java/com/inq/wishhair/wesharewishhair/review/presentation/ReviewSearchApiTest.java index f665174..990f438 100644 --- a/src/test/java/com/inq/wishhair/wesharewishhair/review/presentation/ReviewSearchApiTest.java +++ b/src/test/java/com/inq/wishhair/wesharewishhair/review/presentation/ReviewSearchApiTest.java @@ -12,12 +12,10 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.context.annotation.Import; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -import com.inq.wishhair.wesharewishhair.common.config.EmbeddedRedisConfig; import com.inq.wishhair.wesharewishhair.common.support.ApiTestSupport; import com.inq.wishhair.wesharewishhair.hairstyle.domain.HairStyle; import com.inq.wishhair.wesharewishhair.hairstyle.domain.HairStyleRepository; @@ -29,7 +27,6 @@ import com.inq.wishhair.wesharewishhair.user.domain.entity.User; @DisplayName("[ReviewSearchController 테스트]") -@Import(EmbeddedRedisConfig.class) class ReviewSearchApiTest extends ApiTestSupport { @Autowired diff --git a/src/test/resources/application-test.yml b/src/test/resources/application-test.yml index 2ad932c..8d33432 100644 --- a/src/test/resources/application-test.yml +++ b/src/test/resources/application-test.yml @@ -1,9 +1,7 @@ spring: datasource: - driver-class-name: org.h2.Driver - url: jdbc:h2:mem:test_db;MODE=MySQL; - username: sa - password: + driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver + url: jdbc:tc:mysql:8:/// jpa: hibernate: