Skip to content

Commit

Permalink
fix: @testcontainers를 사용하여 test용 redis 사용하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjookang committed Nov 3, 2024
1 parent 0ad3913 commit e5b004a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,11 @@ dependencies {

//test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.junit.jupiter:junit-jupiter:5.7.1'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
testImplementation 'com.h2database:h2'
testImplementation "org.testcontainers:testcontainers:1.19.0"
testImplementation "org.testcontainers:junit-jupiter:1.19.0"

implementation 'com.squareup.okhttp3:okhttp:4.9.3'
implementation 'com.google.code.gson:gson:2.9.0'
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/server/poptato/auth/api/AuthControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import server.poptato.auth.api.request.KakaoLoginRequestDto;
import server.poptato.auth.application.service.AuthService;
import server.poptato.auth.application.service.JwtService;
Expand All @@ -24,6 +28,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@Testcontainers
@SpringBootTest
@AutoConfigureMockMvc
public class AuthControllerTest {
Expand All @@ -44,6 +49,12 @@ public class AuthControllerTest {
private String refreshToken;
private final String userId = "1";

@Container
private static final GenericContainer<?> redisContainer =
new GenericContainer<>("redis:latest")
.withExposedPorts(6379)
.waitingFor(Wait.forListeningPort());

@BeforeEach
void createAccessToken_UserIdIsOne() {
tokenPair = jwtService.generateTokenPair(userId);
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/server/poptato/auth/application/AuthServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import server.poptato.auth.api.request.ReissueTokenRequestDto;
import server.poptato.auth.application.service.AuthService;
import server.poptato.auth.application.service.JwtService;
Expand All @@ -23,6 +27,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static server.poptato.auth.exception.errorcode.AuthExceptionErrorCode.INVALID_TOKEN;

@Testcontainers
@SpringBootTest
public class AuthServiceTest {
@Autowired
Expand All @@ -45,6 +50,12 @@ public class AuthServiceTest {
@Autowired
private RedisTemplate<String, String> redisTemplate;

@Container
private static final GenericContainer<?> redisContainer =
new GenericContainer<>("redis:latest")
.withExposedPorts(6379)
.waitingFor(Wait.forListeningPort());

@BeforeEach
public void setup() {
TokenPair tokenPair = jwtService.generateTokenPair(userIdTypeString);
Expand Down

0 comments on commit e5b004a

Please sign in to comment.