Skip to content

Commit

Permalink
Merge pull request #508 from woowacourse-teams/feature/#476
Browse files Browse the repository at this point in the history
[feat] 테스트 컨테이너 도입 (#476)
  • Loading branch information
Jaeyoung22 authored Oct 18, 2023
2 parents 36a4fd9 + 794724b commit 165d021
Show file tree
Hide file tree
Showing 31 changed files with 255 additions and 192 deletions.
3 changes: 3 additions & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ dependencies {
// test
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'io.rest-assured:rest-assured:5.3.0'
testImplementation "org.testcontainers:testcontainers:1.19.1"
testImplementation 'org.testcontainers:junit-jupiter:1.19.1'
testImplementation 'org.testcontainers:mysql'

// flyway
implementation 'org.flywaydb:flyway-core'
Expand Down
11 changes: 6 additions & 5 deletions backend/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
spring:
datasource:
url: jdbc:h2:mem:testdb;MODE=MySQL
username: sa
password:
driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
url: jdbc:tc:mysql:8://test
username: root
password: test

task:
execution:
Expand All @@ -25,10 +26,10 @@ spring:
highlight_sql: true
dialect: org.hibernate.dialect.MySQLDialect
hibernate:
ddl-auto: create
ddl-auto: validate

flyway:
enabled: false
enabled: true

logging:
level:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@
import dev.tripdraw.admin.domain.AdminSessionRepository;
import dev.tripdraw.admin.dto.AdminLoginRequest;
import dev.tripdraw.admin.exception.AdminException;
import dev.tripdraw.test.ServiceTest;
import java.time.LocalDateTime;
import java.util.UUID;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;

@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@Transactional
@SpringBootTest
@ServiceTest
class AdminAuthServiceTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,18 @@
import dev.tripdraw.member.domain.MemberRepository;
import dev.tripdraw.post.domain.Post;
import dev.tripdraw.post.domain.PostRepository;
import dev.tripdraw.test.ServiceTest;
import dev.tripdraw.trip.domain.Point;
import dev.tripdraw.trip.domain.PointRepository;
import dev.tripdraw.trip.domain.Trip;
import dev.tripdraw.trip.domain.TripRepository;
import java.time.LocalDateTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;

@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@Transactional
@SpringBootTest
@ServiceTest
class AdminServiceTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package dev.tripdraw.admin.presentation;

import static dev.tripdraw.test.fixture.MemberFixture.사용자;
import static dev.tripdraw.test.fixture.MemberFixture.새로운_사용자;
import static dev.tripdraw.test.fixture.PointFixture.새로운_위치정보;
import static dev.tripdraw.test.fixture.PostFixture.새로운_감상;
import static dev.tripdraw.test.fixture.TripFixture.새로운_여행;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.SoftAssertions.assertSoftly;
import static org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace.NONE;
import static org.springframework.http.HttpStatus.NO_CONTENT;
import static org.springframework.http.HttpStatus.OK;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
Expand Down Expand Up @@ -38,9 +40,8 @@
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;

@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class AdminControllerTest extends ControllerTest {

@Autowired
Expand Down Expand Up @@ -68,7 +69,7 @@ class AdminControllerTest extends ControllerTest {
@BeforeEach
public void setUp() {
super.setUp();
member = memberRepository.save(새로운_사용자("통후추"));
member = memberRepository.save(사용자());
admin = adminRepository.save(new Admin("[email protected]", passwordEncoder.encode("password")));
sessionId = loginRequest(new AdminLoginRequest(admin.email(), "password")).sessionId();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,15 @@
import dev.tripdraw.auth.oauth.OauthClientProvider;
import dev.tripdraw.member.domain.Member;
import dev.tripdraw.member.domain.MemberRepository;
import dev.tripdraw.test.ServiceTest;
import dev.tripdraw.test.TestKakaoApiClient;
import jakarta.transaction.Transactional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
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.mock.mockito.MockBean;

@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@Transactional
@SpringBootTest
@ServiceTest
class AuthServiceFacadeTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,15 @@
import dev.tripdraw.member.domain.Member;
import dev.tripdraw.member.domain.MemberRepository;
import dev.tripdraw.member.exception.MemberException;
import dev.tripdraw.test.ServiceTest;
import java.util.Optional;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;

@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@Transactional
@SpringBootTest
@ServiceTest
class AuthServiceTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,13 @@
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;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.web.server.LocalServerPort;

@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class AuthControllerTest extends ControllerTest {

@LocalServerPort
private int port;

@MockBean
private OauthClientProvider oauthClientProvider;

Expand All @@ -61,7 +53,7 @@ class AuthControllerTest extends ControllerTest {

@BeforeEach
public void setUp() {
RestAssured.port = port;
super.setUp();
given(oauthClientProvider.provide(KAKAO)).willReturn(new TestKakaoApiClient());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package dev.tripdraw.common.profile.presentation;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.http.HttpStatus.OK;

import dev.tripdraw.test.ControllerTest;
import io.restassured.RestAssured;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.http.HttpStatus.OK;

class ProfileControllerTest extends ControllerTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,16 @@
import dev.tripdraw.member.domain.MemberDeleteEvent;
import dev.tripdraw.member.domain.MemberRepository;
import dev.tripdraw.member.dto.MemberSearchResponse;
import jakarta.transaction.Transactional;
import dev.tripdraw.test.ServiceTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.event.ApplicationEvents;
import org.springframework.test.context.event.RecordApplicationEvents;

@RecordApplicationEvents
@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@Transactional
@SpringBootTest
@RecordApplicationEvents
@ServiceTest
class MemberServiceTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.server.LocalServerPort;

@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class MemberControllerTest extends ControllerTest {

@LocalServerPort
int port;

@Autowired
MemberRepository memberRepository;

Expand All @@ -35,7 +30,7 @@ class MemberControllerTest extends ControllerTest {

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

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,22 @@
import dev.tripdraw.post.dto.PostsResponse;
import dev.tripdraw.post.dto.PostsSearchResponse;
import dev.tripdraw.post.exception.PostException;
import dev.tripdraw.test.ServiceTest;
import dev.tripdraw.trip.domain.Point;
import dev.tripdraw.trip.domain.PointRepository;
import dev.tripdraw.trip.domain.Trip;
import dev.tripdraw.trip.domain.TripRepository;
import dev.tripdraw.trip.exception.TripException;
import jakarta.transaction.Transactional;
import java.time.LocalDateTime;
import java.util.Set;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
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.mock.mockito.MockBean;
import org.springframework.web.multipart.MultipartFile;

@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@Transactional
@SpringBootTest
@ServiceTest
class PostServiceTest {

@Autowired
Expand Down Expand Up @@ -200,8 +195,8 @@ void setUp() {
@Test
void 특정_여행의_모든_감상을_조회한다() {
// given
Point point1 = pointRepository.save(새로운_위치정보(2023, 7, 12, 15, 29));
Point point2 = pointRepository.save(새로운_위치정보(2023, 7, 12, 15, 30));
Point point1 = pointRepository.save(새로운_위치정보(2023, 7, 12, 15, 29, trip));
Point point2 = pointRepository.save(새로운_위치정보(2023, 7, 12, 15, 30, trip));
Post post1 = postRepository.save(새로운_감상(point1, member, "제주특별자치도 제주시 애월읍", trip.id()));
Post post2 = postRepository.save(새로운_감상(point2, member, "제주특별자치도 제주시 애월읍", trip.id()));

Expand All @@ -226,9 +221,9 @@ void setUp() {
@Test
void 주소로_모든_여행을_조회한다() {
// given
Point mayPoint = pointRepository.save(새로운_위치정보(2023, 5, 12, 15, 30));
Point julyPoint1 = pointRepository.save(새로운_위치정보(2023, 7, 12, 15, 30));
Point julyPoint2 = pointRepository.save(새로운_위치정보(2023, 7, 12, 15, 30));
Point mayPoint = pointRepository.save(새로운_위치정보(2023, 5, 12, 15, 30, trip));
Point julyPoint1 = pointRepository.save(새로운_위치정보(2023, 7, 12, 15, 30, trip));
Point julyPoint2 = pointRepository.save(새로운_위치정보(2023, 7, 12, 15, 30, trip));
Post jejuMayPost = postRepository.save(새로운_감상(mayPoint, member.id(), "제주특별자치도 제주시 애월읍"));
Post jejuJulyPost = postRepository.save(새로운_감상(julyPoint1, member.id(), "제주특별자치도 제주시 애월읍"));
Post seoulJulyPost = postRepository.save(새로운_감상(julyPoint2, member.id(), "서울특별시 송파구 문정동"));
Expand All @@ -251,9 +246,9 @@ void setUp() {
@Test
void 기간으로_진행된_여행을_조회한다() {
// given
Point mayPoint = pointRepository.save(새로운_위치정보(2023, 5, 12, 15, 30));
Point julyPoint1 = pointRepository.save(새로운_위치정보(2023, 7, 12, 15, 30));
Point julyPoint2 = pointRepository.save(새로운_위치정보(2023, 7, 12, 15, 30));
Point mayPoint = pointRepository.save(새로운_위치정보(2023, 5, 12, 15, 30, trip));
Point julyPoint1 = pointRepository.save(새로운_위치정보(2023, 7, 12, 15, 30, trip));
Point julyPoint2 = pointRepository.save(새로운_위치정보(2023, 7, 12, 15, 30, trip));
Post jejuMayPost = postRepository.save(새로운_감상(mayPoint, member.id(), "제주특별자치도 제주시 애월읍"));
Post jejuJulyPost = postRepository.save(새로운_감상(julyPoint1, member.id(), "제주특별자치도 제주시 애월읍"));
Post seoulJulyPost = postRepository.save(새로운_감상(julyPoint2, member.id(), "서울특별시 송파구 문정동"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static dev.tripdraw.test.fixture.MemberFixture.사용자;
import static dev.tripdraw.test.fixture.PointFixture.새로운_위치정보;
import static dev.tripdraw.test.fixture.PostFixture.새로운_감상;
import static dev.tripdraw.test.fixture.TripFixture.새로운_여행;
import static org.assertj.core.api.Assertions.assertThat;

import dev.tripdraw.common.config.JpaConfig;
Expand All @@ -13,6 +14,8 @@
import dev.tripdraw.post.dto.PostSearchConditions;
import dev.tripdraw.trip.domain.Point;
import dev.tripdraw.trip.domain.PointRepository;
import dev.tripdraw.trip.domain.Trip;
import dev.tripdraw.trip.domain.TripRepository;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -43,13 +46,17 @@ class PostDynamicQueryRepositoryTest {
@Autowired
private PointRepository pointRepository;

@Autowired
private TripRepository tripRepository;

@Test
void 조건에_해당하는_감상을_조회한다() {
// given
Member member = memberRepository.save(사용자());
Point mayPoint = pointRepository.save(새로운_위치정보(2023, 5, 12, 15, 30));
Point julyPoint1 = pointRepository.save(새로운_위치정보(2023, 7, 12, 15, 30));
Point julyPoint2 = pointRepository.save(새로운_위치정보(2023, 7, 12, 15, 30));
Trip trip = tripRepository.save(새로운_여행(member));
Point mayPoint = pointRepository.save(새로운_위치정보(2023, 5, 12, 15, 30, trip));
Point julyPoint1 = pointRepository.save(새로운_위치정보(2023, 7, 12, 15, 30, trip));
Point julyPoint2 = pointRepository.save(새로운_위치정보(2023, 7, 12, 15, 30, trip));
Post jejuMayPost = postRepository.save(새로운_감상(mayPoint, member.id(), "제주특별자치도 제주시 애월읍"));
Post jejuJulyPost = postRepository.save(새로운_감상(julyPoint1, member.id(), "제주특별자치도 제주시 애월읍"));
Post seoulJulyPost = postRepository.save(새로운_감상(julyPoint2, member.id(), "서울특별시 송파구 문정동"));
Expand All @@ -70,9 +77,10 @@ class PostDynamicQueryRepositoryTest {
void 감상을_전체_조회한다() {
// given
Member member = memberRepository.save(사용자());
Trip trip = tripRepository.save(새로운_여행(member));
for (int i = 1; i <= 5; i++) {
Point point = pointRepository.save(새로운_위치정보(LocalDateTime.now()));
postRepository.save(새로운_감상(point, member.id(), "주소 " + i));
Point point = pointRepository.save(새로운_위치정보(LocalDateTime.now(), trip));
postRepository.save(새로운_감상(point, member.id(), "주소 " + i, trip.id()));
}
PostPaging postPaging = new PostPaging(null, 3);

Expand Down
Loading

0 comments on commit 165d021

Please sign in to comment.