Skip to content

Commit

Permalink
test: TestContainers 적용 (#583)
Browse files Browse the repository at this point in the history
* feat: TestContainers 라이브러리 적용하여 MySQL 컨테이너 설정

* test: ControllerTest와 ServiceTest에 MySql Container 사용한도록 하는 스프링 컨텍스트 구현

* test: ControllerTest와 ServiceTest에 MySql Container 사용한도록 하는 스프링 컨텍스트 적용

* chore: DEV 브랜치 CI/CD 워크플로우 수정
  • Loading branch information
dooboocookie authored Dec 7, 2023
1 parent 1a45f38 commit eb385e5
Show file tree
Hide file tree
Showing 26 changed files with 129 additions and 59 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/backend_dev_merge_workflow.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
name: NAAGA BACKEND MERGE CI
name: NAAGA BACKEND PRODUCT SERVER MERGE CI/CD

on:
push:
branches:
- dev_backend

jobs:
github_actions_setting:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -41,18 +42,17 @@ jobs:
- name: 🐳 도커 이미지 빌드 중... 🐳
run: |
cd backend
docker build --platform linux/arm64/v8 -t ${{ secrets.DOCKERHUB_REPOSITORY }}/${{ secrets.DOCKERHUB_APPNAME }} -f Dockerfile-dev .
docker build --platform linux/arm64/v8 -t ${{ secrets.DOCKERHUB_REPOSITORY }}/${{ secrets.DOCKERHUB_APPNAME }} -f Dockerfile-prod .
- name: 🐳 도커 허브에 Push 중... 🐳
run: docker push ${{ secrets.DOCKERHUB_REPOSITORY }}/${{ secrets.DOCKERHUB_APPNAME }}

naaga_dev_ec2_deploy:
naaga_prod_ec2_deploy:
needs: github_actions_setting
runs-on: naaga

steps:
- name: 🙏 쉘 스크립트 실행 중 ... 🙏
run: |
cd /home/ubuntu
sudo ./deploy_new.sh
cd /home/ubuntu/prod
sudo ./deploy_prod.sh
1 change: 1 addition & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies {

testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.rest-assured:rest-assured:5.3.1'
testImplementation 'org.testcontainers:mysql:1.19.2'
}

tasks.named('test') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.now.naaga.auth.infrastructure.AuthType;
import com.now.naaga.auth.infrastructure.dto.AuthInfo;
import com.now.naaga.auth.infrastructure.dto.MemberAuth;
import com.now.naaga.common.ServiceTest;
import com.now.naaga.common.MySqlContainerServiceTest;
import com.now.naaga.member.domain.Member;
import com.now.naaga.player.domain.Player;
import com.now.naaga.score.domain.Score;
Expand All @@ -21,7 +21,7 @@
import org.springframework.beans.factory.annotation.Autowired;

@SuppressWarnings("NonAsciiCharacters")
class AuthServiceTest extends ServiceTest {
class AuthServiceTest extends MySqlContainerServiceTest {

@Autowired
private AuthService authService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.now.naaga.auth.presentation.dto.AuthRequest;
import com.now.naaga.auth.presentation.dto.AuthResponse;
import com.now.naaga.auth.presentation.dto.RefreshTokenRequest;
import com.now.naaga.common.ControllerTest;
import com.now.naaga.common.MySqlContainerControllerTest;
import com.now.naaga.common.exception.ExceptionResponse;
import com.now.naaga.member.domain.Member;
import com.now.naaga.player.domain.Player;
Expand All @@ -31,7 +31,7 @@
import org.springframework.http.MediaType;

@SuppressWarnings("NonAsciiCharacters")
class AuthControllerTest extends ControllerTest {
class AuthControllerTest extends MySqlContainerControllerTest {

@Test
void 이미_존재하는_멤버_정보로_카카오_토큰을_통해서_로그인_요청을_하면_액세스_토큰을_발급한다() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@

import com.now.naaga.auth.domain.AuthToken;
import com.now.naaga.auth.infrastructure.AuthType;
import com.now.naaga.common.ControllerTest;
import com.now.naaga.common.MySqlContainerControllerTest;
import com.now.naaga.common.exception.ExceptionResponse;
import com.now.naaga.member.domain.Member;
import com.now.naaga.player.domain.Player;
import io.restassured.RestAssured;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
import org.junit.jupiter.api.Test;
import org.springframework.http.HttpStatus;

@SuppressWarnings("NonAsciiCharacters")
public class AuthInterceptorTest extends ControllerTest {
public class AuthInterceptorTest extends MySqlContainerControllerTest {

@Test
void 인증_헤더_정보가_존재하지_않을__401_응답한다() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import com.now.naaga.auth.domain.AuthToken;
import com.now.naaga.auth.infrastructure.AuthType;
import com.now.naaga.common.ControllerTest;
import com.now.naaga.common.MySqlContainerControllerTest;
import com.now.naaga.common.exception.ExceptionResponse;
import com.now.naaga.player.domain.Player;
import io.restassured.RestAssured;
Expand All @@ -16,7 +16,7 @@
import org.springframework.http.HttpStatus;

@SuppressWarnings("NonAsciiCharacters")
public class PlayerArgumentResolverTest extends ControllerTest {
public class PlayerArgumentResolverTest extends MySqlContainerControllerTest {

@Test
void 인증_헤더의_토큰_정보가_존재하지_않는_멤버일__예외를_발생한다() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.now.naaga.auth.exception.AuthException;
import com.now.naaga.common.ControllerTest;
import com.now.naaga.common.MySqlContainerControllerTest;
import java.util.Base64;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
Expand All @@ -15,7 +15,7 @@
import org.springframework.web.servlet.mvc.Controller;

@SuppressWarnings("NonAsciiCharacters")
class ManagerAuthInterceptorTest extends ControllerTest {
class ManagerAuthInterceptorTest extends MySqlContainerControllerTest {

@Value("${manager.id}")
String id;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.now.naaga.common;

import com.now.naaga.auth.domain.AuthToken;
import com.now.naaga.auth.infrastructure.AuthType;
import com.now.naaga.auth.infrastructure.jwt.AuthTokenGenerator;
import com.now.naaga.auth.infrastructure.jwt.JwtProvider;
import com.now.naaga.member.domain.Member;
import com.now.naaga.player.domain.Player;
import io.restassured.RestAssured;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import org.junit.jupiter.api.BeforeEach;
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;

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public abstract class MySqlContainerControllerTest extends MySqlContainerTest {

@Autowired
protected AuthTokenGenerator authTokenGenerator;

@Autowired
protected JwtProvider jwtProvider;

@LocalServerPort
private int port;

@BeforeEach
void setUp() {
RestAssured.port = port;
}

protected String authorizationForBearer(final Player player) {
final Member member = player.getMember();
final AuthToken generate = authTokenGenerator.generate(member, member.getId(), AuthType.KAKAO);
final String accessToken = generate.getAccessToken();
return "Bearer " + accessToken;
}

protected String authorizationForBearer(final Member member) {
final AuthToken generate = authTokenGenerator.generate(member, member.getId(), AuthType.KAKAO);
final String accessToken = generate.getAccessToken();
return "Bearer " + accessToken;
}

protected Long getIdFromLocationHeader(ExtractableResponse<Response> extractableResponse) {
String[] split = extractableResponse.header("Location").split("/");
return Long.parseLong(split[split.length - 1]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.now.naaga.common;

import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public abstract class MySqlContainerServiceTest extends MySqlContainerTest {

}
19 changes: 19 additions & 0 deletions backend/src/test/java/com/now/naaga/common/MySqlContainerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.now.naaga.common;

import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.MySQLContainer;

public abstract class MySqlContainerTest extends AbstractTest {

static final MySQLContainer<?> mySqlContainer = new MySQLContainer<>("mysql:8.0.35");

@DynamicPropertySource
static void mySqlProperties(final DynamicPropertyRegistry registry) {
mySqlContainer.start();
registry.add("spring.datasource.url", mySqlContainer::getJdbcUrl);
registry.add("spring.datasource.username", mySqlContainer::getUsername);
registry.add("spring.datasource.password", mySqlContainer::getPassword);
registry.add("spring.datasource.driver-class-name", mySqlContainer::getDriverClassName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.now.naaga.common.ServiceTest;
import com.now.naaga.common.MySqlContainerServiceTest;
import com.now.naaga.game.application.dto.CreateGameCommand;
import com.now.naaga.game.application.dto.EndGameCommand;
import com.now.naaga.game.application.dto.FindGameByIdCommand;
Expand All @@ -40,7 +40,7 @@
import org.springframework.beans.factory.annotation.Autowired;

@SuppressWarnings("NonAsciiCharacters")
class GameServiceTest extends ServiceTest {
class GameServiceTest extends MySqlContainerServiceTest {

@Autowired
private GameService gameService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.now.naaga.common.ServiceTest;
import com.now.naaga.common.MySqlContainerServiceTest;
import com.now.naaga.common.exception.BaseExceptionType;
import com.now.naaga.game.application.dto.CreateHintCommand;
import com.now.naaga.game.application.dto.FindHintByIdCommand;
Expand All @@ -23,7 +23,7 @@
import org.springframework.transaction.annotation.Transactional;

@SuppressWarnings("NonAsciiCharacters")
class HintServiceTest extends ServiceTest {
class HintServiceTest extends MySqlContainerServiceTest {

@Autowired
private HintService hintService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import static com.now.naaga.gameresult.domain.ResultType.SUCCESS;
import static org.assertj.core.api.SoftAssertions.assertSoftly;

import com.now.naaga.common.ControllerTest;
import com.now.naaga.common.MySqlContainerControllerTest;
import com.now.naaga.common.exception.ExceptionResponse;
import com.now.naaga.game.domain.Direction;
import com.now.naaga.game.domain.Game;
Expand Down Expand Up @@ -54,7 +54,7 @@
import org.springframework.http.MediaType;

@SuppressWarnings("NonAsciiCharacters")
class GameControllerTest extends ControllerTest {
class GameControllerTest extends MySqlContainerControllerTest {

@Test
void 게임_생성_요청시_진행중인_게임이_없으면서_주변에_추천_장소가_있다면_게임을_정상적으로_생성한다() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static com.now.naaga.common.fixture.PositionFixture.잠실역_교보문고_좌표;
import static org.assertj.core.api.SoftAssertions.assertSoftly;

import com.now.naaga.common.ControllerTest;
import com.now.naaga.common.MySqlContainerControllerTest;
import com.now.naaga.game.application.GameService;
import com.now.naaga.game.application.dto.EndGameCommand;
import com.now.naaga.game.domain.EndType;
Expand All @@ -23,7 +23,7 @@
import org.springframework.http.MediaType;

@SuppressWarnings("NonAsciiCharacters")
public class StatisticControllerTest extends ControllerTest {
public class StatisticControllerTest extends MySqlContainerControllerTest {

@Autowired
private GameService gameService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import static com.now.naaga.gameresult.domain.ResultType.SUCCESS;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import com.now.naaga.common.ServiceTest;
import com.now.naaga.common.MySqlContainerServiceTest;
import com.now.naaga.game.application.dto.CreateGameResultCommand;
import com.now.naaga.game.domain.Game;
import com.now.naaga.game.exception.GameException;
Expand All @@ -21,7 +21,7 @@
import org.springframework.transaction.annotation.Transactional;

@SuppressWarnings("NonAsciiCharacters")
class GameResultServiceTest extends ServiceTest {
class GameResultServiceTest extends MySqlContainerServiceTest {

@Autowired
private GameResultService gameResultService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

import com.now.naaga.common.ServiceTest;
import com.now.naaga.common.MySqlContainerServiceTest;
import com.now.naaga.common.exception.BaseExceptionType;
import com.now.naaga.game.domain.Game;
import com.now.naaga.game.domain.GameStatus;
Expand All @@ -35,7 +35,7 @@
import org.springframework.transaction.annotation.Transactional;

@SuppressWarnings("NonAsciiCharacters")
class LetterServiceTest extends ServiceTest {
class LetterServiceTest extends MySqlContainerServiceTest {

@Autowired
private LetterService letterService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import static com.now.naaga.player.exception.PlayerExceptionType.PLAYER_NOT_FOUND;
import static org.assertj.core.api.SoftAssertions.assertSoftly;

import com.now.naaga.common.ControllerTest;
import com.now.naaga.common.MySqlContainerControllerTest;
import com.now.naaga.common.exception.ExceptionResponse;
import com.now.naaga.game.domain.Game;
import com.now.naaga.game.presentation.dto.CoordinateResponse;
Expand Down Expand Up @@ -40,7 +40,7 @@
import org.springframework.http.MediaType;

@SuppressWarnings("NonAsciiCharacters")
class LetterControllerTest extends ControllerTest {
class LetterControllerTest extends MySqlContainerControllerTest {

@Test
void 주변_쪽지를_모두_조회한다() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.now.naaga.common.ServiceTest;
import com.now.naaga.common.MySqlContainerServiceTest;
import com.now.naaga.common.exception.BaseExceptionType;
import com.now.naaga.like.application.dto.ApplyLikeCommand;
import com.now.naaga.like.application.dto.CancelLikeCommand;
Expand All @@ -31,7 +31,7 @@
import org.springframework.transaction.annotation.Transactional;

@SuppressWarnings("NonAsciiCharacters")
class PlaceLikeServiceTest extends ServiceTest {
class PlaceLikeServiceTest extends MySqlContainerServiceTest {

@Autowired
private PlaceLikeService placeLikeService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import static org.springframework.http.HttpStatus.NO_CONTENT;
import static org.springframework.http.HttpStatus.OK;

import com.now.naaga.common.ControllerTest;
import com.now.naaga.common.MySqlContainerControllerTest;
import com.now.naaga.common.exception.ExceptionResponse;
import com.now.naaga.like.domain.MyPlaceLikeType;
import com.now.naaga.like.domain.PlaceLike;
Expand All @@ -30,7 +30,7 @@


@SuppressWarnings("NonAsciiCharacters")
class PlaceLikeControllerTest extends ControllerTest {
class PlaceLikeControllerTest extends MySqlContainerControllerTest {

@Test
void 좋아요_등록이_성공하면_201_응답을_반환한다() {
Expand Down
Loading

0 comments on commit eb385e5

Please sign in to comment.