Skip to content

Commit

Permalink
refactor: 펫푸드 테스트코드 리팩터링 (#459)
Browse files Browse the repository at this point in the history
* refactor: PetFoodQueryRepository 테스트간 독립성 보장

* refactor: PetFoodQueryService 테스트간 독립성 보장

* refactor: PetFoodController 테스트간 독립성 보장

* refactor: PetFoodQueryRepository 테스트 수정

* refactor: fixture 수정

* refactor: PetFoodQueryService 테스트 수정

* refactor: PetFoodController 테스트 수정

* refactor: QueryServiceTest 삭제

* refactor: 테스트 환경 통합 및 불필요한 컨테이너 제거

* refactor: abstract 추가
  • Loading branch information
parkmuhyeun authored Oct 12, 2023
1 parent a59c14a commit 35a8c6f
Show file tree
Hide file tree
Showing 24 changed files with 396 additions and 309 deletions.
13 changes: 0 additions & 13 deletions backend/src/test/java/zipgo/ZipgoApplicationTests.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
package zipgo.admin.application;

import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import zipgo.admin.dto.FunctionalitySelectResponse;
import zipgo.admin.dto.PetFoodReadResponse;
import zipgo.admin.dto.PrimaryIngredientSelectResponse;
import zipgo.brand.domain.Brand;
import zipgo.brand.domain.fixture.BrandFixture;
import zipgo.brand.domain.repository.BrandRepository;
import zipgo.common.service.QueryServiceTest;
import zipgo.common.service.ServiceTest;
import zipgo.petfood.domain.PetFood;
import zipgo.petfood.domain.fixture.FunctionalityFixture;
import zipgo.petfood.domain.fixture.PetFoodFixture;
import zipgo.petfood.domain.fixture.PrimaryIngredientFixture;
import zipgo.petfood.domain.repository.FunctionalityRepository;
import zipgo.petfood.domain.repository.PetFoodRepository;
import zipgo.petfood.domain.repository.PrimaryIngredientRepository;
import zipgo.admin.dto.PrimaryIngredientSelectResponse;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

class AdminQueryServiceTest extends QueryServiceTest {
class AdminQueryServiceTest extends ServiceTest {

@Autowired
private FunctionalityRepository functionalityRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,20 @@
package zipgo.admin.presentation;

import com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.nio.charset.StandardCharsets;
import java.util.List;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
import zipgo.admin.application.AdminQueryService;
import zipgo.admin.application.AdminService;
import zipgo.common.acceptance.MockMvcTest;
import zipgo.admin.dto.BrandCreateRequest;
import zipgo.admin.dto.PetFoodCreateRequest;
import zipgo.auth.presentation.JwtMandatoryArgumentResolver;
import zipgo.common.acceptance.MockMvcTest;
import zipgo.image.application.ImageService;
import zipgo.petfood.domain.fixture.PetFoodFixture;

import java.nio.charset.StandardCharsets;
import java.util.List;

import static com.epages.restdocs.apispec.RestAssuredRestDocumentationWrapper.resourceDetails;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.multipart;
Expand All @@ -31,23 +24,7 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static zipgo.brand.domain.fixture.BrandFixture.무민_브랜드_생성_요청;

@WebMvcTest(controllers = AdminController.class)
class AdminControllerMockTest extends MockMvcTest {

@Autowired
private ObjectMapper objectMapper;

@MockBean
private ImageService imageService;

@MockBean
private AdminService adminService;

@MockBean
private AdminQueryService adminQueryService;

@MockBean
private JwtMandatoryArgumentResolver argumentResolver;
class AdminControllerMockMvcTest extends MockMvcTest {

@Test
void 브랜드를_생성하면_201이_반환된다() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
package zipgo.auth.presentation;

import java.util.List;

import com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper;
import com.epages.restdocs.apispec.ResourceSnippetDetails;
import com.epages.restdocs.apispec.Schema;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.ResponseCookie;
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
import org.springframework.restdocs.payload.JsonFieldType;
import zipgo.auth.application.AuthService;
import zipgo.auth.dto.TokenDto;
import zipgo.auth.exception.OAuthTokenNotBringException;
import zipgo.auth.support.RefreshTokenCookieProvider;
import zipgo.common.acceptance.MockMvcTest;
import zipgo.member.application.MemberQueryService;
import zipgo.pet.application.PetQueryService;
import zipgo.pet.domain.fixture.PetFixture;

import java.util.List;

import static com.epages.restdocs.apispec.RestAssuredRestDocumentationWrapper.resourceDetails;
import static java.util.Collections.EMPTY_LIST;
import static org.mockito.Mockito.when;
Expand All @@ -31,26 +25,13 @@
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static zipgo.member.domain.fixture.MemberFixture.식별자_있는_멤버;

@WebMvcTest(controllers = AuthController.class)
class AuthControllerMockTest extends MockMvcTest {

private static final Schema 응답_형식 = Schema.schema("TokenResponse");
private static final ResourceSnippetDetails 문서_정보 = resourceDetails().summary("로그인")
.description("로그인 합니다.")
.responseSchema(응답_형식);

@MockBean
private MemberQueryService memberQueryService;

@MockBean
private PetQueryService petQueryService;

@MockBean
private RefreshTokenCookieProvider refreshTokenCookieProvider;

@MockBean
private AuthService authService;

@Test
void 로그인_성공() throws Exception {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import zipgo.brand.domain.Brand;
import zipgo.brand.domain.fixture.BrandFixture;
import zipgo.brand.domain.repository.BrandRepository;
import zipgo.common.service.QueryServiceTest;
import zipgo.common.service.ServiceTest;

import static org.assertj.core.api.Assertions.assertThat;

class BrandQueryServiceTest extends QueryServiceTest {
class BrandQueryServiceTest extends ServiceTest {

@Autowired
private BrandRepository brandRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import static org.springframework.test.context.jdbc.Sql.ExecutionPhase.AFTER_TEST_METHOD;

@SuppressWarnings("NonAsciiCharacters")
@Sql(scripts = {"classpath:truncate.sql"}, executionPhase = AFTER_TEST_METHOD)
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Sql(scripts = {"classpath:truncate.sql"}, executionPhase = AFTER_TEST_METHOD)
public abstract class AcceptanceTest {

protected RequestSpecification spec;
Expand Down
53 changes: 51 additions & 2 deletions backend/src/test/java/zipgo/common/acceptance/MockMvcTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package zipgo.common.acceptance;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -9,22 +10,70 @@
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import zipgo.admin.application.AdminQueryService;
import zipgo.admin.application.AdminService;
import zipgo.admin.presentation.AdminController;
import zipgo.aspect.QueryCounter;
import zipgo.auth.application.AuthService;
import zipgo.auth.presentation.AuthController;
import zipgo.auth.presentation.AuthInterceptor;
import zipgo.auth.presentation.JwtMandatoryArgumentResolver;
import zipgo.auth.support.JwtProvider;
import zipgo.auth.support.RefreshTokenCookieProvider;
import zipgo.image.application.ImageService;
import zipgo.image.presentaion.ImageController;
import zipgo.member.application.MemberQueryService;
import zipgo.pet.application.PetQueryService;

@WebMvcTest
@AutoConfigureRestDocs
@ExtendWith(SpringExtension.class)
@SuppressWarnings("NonAsciiCharacters")
@WebMvcTest(
controllers = {
AdminController.class,
AuthController.class,
ImageController.class
}
)
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
public class MockMvcTest {
public abstract class MockMvcTest {

@Autowired
protected ObjectMapper objectMapper;

@Autowired
protected MockMvc mockMvc;

@MockBean
protected ImageService imageService;

@MockBean
protected AdminService adminService;

@MockBean
protected AdminQueryService adminQueryService;

@MockBean
protected JwtProvider jwtProvider;

@MockBean
protected AuthInterceptor authInterceptor;

@MockBean
protected JwtMandatoryArgumentResolver argumentResolver;

@MockBean
protected MemberQueryService memberQueryService;

@MockBean
protected PetQueryService petQueryService;

@MockBean
protected AuthService authService;

@MockBean
protected RefreshTokenCookieProvider refreshTokenCookieProvider;

@MockBean
protected QueryCounter queryCounter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;
import zipgo.common.config.JpaConfig;
import zipgo.common.config.QueryDslTestConfig;


@DataJpaTest
@Import(JpaConfig.class)
@Import({
JpaConfig.class,
QueryDslTestConfig.class
})
@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
public class RepositoryTest {
public abstract class RepositoryTest {

}
14 changes: 0 additions & 14 deletions backend/src/test/java/zipgo/common/service/QueryServiceTest.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
@SuppressWarnings("NonAsciiCharacters")
@SpringBootTest(properties = {"spring.sql.init.mode=never"})
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
public class ServiceTest {
public abstract class ServiceTest {

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@
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.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.MockMultipartFile;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.when;


@SpringBootTest
@ExtendWith(MockitoExtension.class)
@SuppressWarnings("NonAsciiCharacters")
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
class ImageServiceTest {

@MockBean
@Mock
private ImageClient imageClient;

@Autowired
@InjectMocks
private ImageService imageService;

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@

import com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.restdocs.mockmvc.RestDocumentationResultHandler;
import org.springframework.restdocs.payload.JsonFieldType;
import org.springframework.test.web.servlet.MockMvc;
import zipgo.auth.presentation.JwtMandatoryArgumentResolver;
import zipgo.common.acceptance.MockMvcTest;
import zipgo.image.ImageDirectoryUrl;
import zipgo.image.application.ImageService;

import static com.epages.restdocs.apispec.RestAssuredRestDocumentationWrapper.resourceDetails;
import static org.mockito.Mockito.when;
Expand All @@ -26,17 +20,7 @@
import static org.springframework.restdocs.request.RequestDocumentation.requestParts;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(controllers = ImageController.class)
class ImageControllerTest extends MockMvcTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private ImageService imageService;

@MockBean
private JwtMandatoryArgumentResolver argumentResolver;
class ImageControllerMvcTest extends MockMvcTest {

@Test
void 사진_등록_성공하면_201_반환() throws Exception {
Expand Down
Loading

0 comments on commit 35a8c6f

Please sign in to comment.