Skip to content

Commit

Permalink
enhancement: 테스트 환경 TestContainer 로 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
EunChanNam committed Nov 21, 2023
1 parent 8366f2f commit b86f6e4
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 63 deletions.
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
6 changes: 2 additions & 4 deletions src/test/java/com/inq/wishhair/wesharewishhair/LikeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@SpringBootTest
@AutoConfigureMockMvc
@Transactional
public abstract class ApiTestSupport {
public abstract class ApiTestSupport extends TestContainerSupport {

private final ObjectMapper objectMapper = new ObjectMapper();

Expand Down
Original file line number Diff line number Diff line change
@@ -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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,7 +27,6 @@
import com.inq.wishhair.wesharewishhair.user.domain.entity.User;

@DisplayName("[ReviewSearchController 테스트]")
@Import(EmbeddedRedisConfig.class)
class ReviewSearchApiTest extends ApiTestSupport {

@Autowired
Expand Down
6 changes: 2 additions & 4 deletions src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit b86f6e4

Please sign in to comment.