Skip to content

Commit

Permalink
refactor: 테스트 redis 환경 testContainer으로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
ikjo39 committed Feb 1, 2025
1 parent 6a559de commit fae9915
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 184 deletions.
3 changes: 3 additions & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ dependencies {

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.rest-assured:rest-assured'
testImplementation 'org.testcontainers:junit-jupiter:1.20.4'
testImplementation 'org.testcontainers:testcontainers:1.20.4'
testImplementation 'com.redis.testcontainers:testcontainers-redis:1.6.4'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

Expand Down
66 changes: 0 additions & 66 deletions backend/src/test/java/kr/momo/config/PortKiller.java

This file was deleted.

33 changes: 0 additions & 33 deletions backend/src/test/java/kr/momo/config/TestRedisConfig.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import kr.momo.support.TestContainers;
import kr.momo.domain.attendee.Attendee;
import kr.momo.domain.attendee.AttendeeRepository;
import kr.momo.domain.availabledate.AvailableDate;
Expand All @@ -22,22 +24,22 @@
import kr.momo.service.attendee.dto.AttendeeLoginRequest;
import kr.momo.service.schedule.dto.DateTimesCreateRequest;
import kr.momo.service.schedule.dto.ScheduleCreateRequest;
import kr.momo.support.EnableEmbeddedCache;
import kr.momo.support.IsolateDatabaseAndCache;
import kr.momo.support.IsolateDatabase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.http.HttpStatus;
import org.springframework.security.crypto.password.PasswordEncoder;

@EnableEmbeddedCache
@IsolateDatabaseAndCache
@IsolateDatabase
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
class ScheduleControllerTest {
class ScheduleControllerTest extends TestContainers {

@LocalServerPort
private int port;
Expand All @@ -57,6 +59,9 @@ class ScheduleControllerTest {
@Autowired
private PasswordEncoder passwordEncoder;

@Autowired
private CacheManager cacheManager;

private Meeting meeting;
private AttendeeFixture fixture;
private Attendee attendee;
Expand All @@ -71,6 +76,11 @@ void setUp() {
attendee = attendeeRepository.save(fixture.create(meeting));
today = availableDateRepository.save(new AvailableDate(LocalDate.now(), meeting));
tomorrow = availableDateRepository.save(new AvailableDate(LocalDate.now().plusDays(1), meeting));

for (String name : cacheManager.getCacheNames()) {
Cache cache = cacheManager.getCache(name);
Objects.requireNonNull(cache).clear();
}
}

@DisplayName("참가자가 스케줄을 생성하는데 성공하면 200 상태 코드를 응답한다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,20 @@
import kr.momo.service.schedule.dto.RecommendedSchedulesResponse;
import kr.momo.service.schedule.dto.ScheduleCreateRequest;
import kr.momo.service.schedule.dto.SchedulesResponse;
import kr.momo.support.EnableEmbeddedCache;
import kr.momo.support.IsolateDatabaseAndCache;
import kr.momo.support.IsolateDatabase;
import kr.momo.support.TestContainers;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;

@EnableEmbeddedCache
@IsolateDatabaseAndCache
@IsolateDatabase
@SpringBootTest(webEnvironment = WebEnvironment.NONE)
class ScheduleServiceTest {
class ScheduleServiceTest extends TestContainers {

@Autowired
private ScheduleService scheduleService;
Expand All @@ -69,6 +70,9 @@ class ScheduleServiceTest {
@Autowired
private ScheduleCache scheduleCache;

@Autowired
private CacheManager cacheManager;

private Meeting meeting;
private Attendee attendee;
private AvailableDate today;
Expand All @@ -88,6 +92,11 @@ void setUp() {
new DateTimesCreateRequest(today.getDate(), times),
new DateTimesCreateRequest(tomorrow.getDate(), times)
);

for (String name : cacheManager.getCacheNames()) {
Cache cache = cacheManager.getCache(name);
cache.clear();
}
}

@DisplayName("스케줄 생성 시 사용자의 기존 스케줄들을 모두 삭제하고 새로운 스케줄을 저장한다.")
Expand Down
38 changes: 0 additions & 38 deletions backend/src/test/java/kr/momo/support/CacheCleanupListener.java

This file was deleted.

15 changes: 0 additions & 15 deletions backend/src/test/java/kr/momo/support/EnableEmbeddedCache.java

This file was deleted.

17 changes: 0 additions & 17 deletions backend/src/test/java/kr/momo/support/IsolateDatabaseAndCache.java

This file was deleted.

29 changes: 29 additions & 0 deletions backend/src/test/java/kr/momo/support/TestContainers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package kr.momo.support;

import com.redis.testcontainers.RedisContainer;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

@Testcontainers
public abstract class TestContainers {

private static final String REDIS_IMAGE = "redis:6-alpine";
private static final int REDIS_PORT = 6379;
@Container
private static final RedisContainer REDIS_CONTAINER;

static {
REDIS_CONTAINER = new RedisContainer(DockerImageName.parse(REDIS_IMAGE))
.withExposedPorts(REDIS_PORT);
REDIS_CONTAINER.start();
}

@DynamicPropertySource
private static void databaseProperties(DynamicPropertyRegistry registry) {
registry.add("spring.data.redis.host", REDIS_CONTAINER::getHost);
registry.add("spring.data.redis.port", () -> REDIS_CONTAINER.getMappedPort(REDIS_PORT).toString());
}
}
5 changes: 0 additions & 5 deletions backend/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ spring:
init:
mode: never

data:
redis:
host: localhost
port: 6370

logging:
level:
org.hibernate.orm.jdbc:
Expand Down

0 comments on commit fae9915

Please sign in to comment.