Skip to content

Commit

Permalink
[BE] test: ApplicationContext 재사용을 통해, 테스트 속도 향상 (#639) (#640)
Browse files Browse the repository at this point in the history
* test: Controller 단위 테스트 컨텍스트 재활용하도록 변경

* test: Repository 단위 테스트 컨텍스트 재활용하도록 변경

* test: 서비스 통합 테스트 컨텍스트 재활용하도록 변경

* chore: MockAuthTestExecutionListener 상속에서 구현으로 변경

* fix: No Scope registered for scope name 'request' 수정

* chore: 테스트 클래스 잘못된 패키지 위치 수정

* fix: CustomWebMvcTest 어노테이션 value 복귀, 다른 브랜치에서 충돌 방지
  • Loading branch information
seokjin8678 authored Dec 25, 2023
1 parent 6d48b30 commit 3382f8a
Show file tree
Hide file tree
Showing 26 changed files with 232 additions and 262 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import io.restassured.response.Response;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Component;
import org.springframework.boot.test.context.TestComponent;

@Component
@TestComponent
@ScenarioScope
public class CucumberClient {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
package com.festago.acceptance;

import com.festago.support.DatabaseClearTestExecutionListener;
import io.cucumber.java.Before;
import io.cucumber.spring.CucumberContextConfiguration;
import io.restassured.RestAssured;
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.context.annotation.Import;
import org.springframework.test.context.TestExecutionListeners;

@Import(CucumberClient.class)
@CucumberContextConfiguration
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
@TestExecutionListeners(listeners = {
DatabaseClearTestExecutionListener.class}, mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
public class CucumberSpringConfiguration {

@LocalServerPort
private int port;

@Autowired
private DataInitializer dataInitializer;

@Before
public void before() {
dataInitializer.execute();
RestAssured.port = port;
}
}
83 changes: 0 additions & 83 deletions backend/src/test/java/com/festago/acceptance/DataInitializer.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.festago.auth.application.AdminAuthService;
import com.festago.auth.application.AuthExtractor;
import com.festago.auth.domain.Role;
import com.festago.common.exception.ErrorCode;
import com.festago.common.exception.NotFoundException;
Expand Down Expand Up @@ -44,12 +42,10 @@
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
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.mock.mockito.SpyBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

@CustomWebMvcTest(AdminController.class)
@CustomWebMvcTest
@DisplayNameGeneration(ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
class AdminControllerTest {
Expand All @@ -60,24 +56,21 @@ class AdminControllerTest {
@Autowired
ObjectMapper objectMapper;

@MockBean
@Autowired
FestivalService festivalService;

@MockBean
@Autowired
StageService stageService;

@MockBean
@Autowired
TicketService ticketService;

@MockBean
@Autowired
AdminAuthService adminAuthService;

@MockBean
@Autowired
SchoolService schoolService;

@SpyBean
AuthExtractor authExtractor;

@Test
@WithMockAuth
void 토큰의_Role이_어드민이_아니면_404_NotFound() throws Exception {
Expand Down Expand Up @@ -126,7 +119,6 @@ class AdminControllerTest {
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
.cookie(new Cookie("token", "token")))
.andDo(print())
.andExpect(status().isOk())
.andReturn()
.getResponse()
Expand Down Expand Up @@ -161,7 +153,6 @@ class AdminControllerTest {
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
.cookie(new Cookie("token", "token")))
.andDo(print())
.andExpect(status().isNotFound())
.andReturn()
.getResponse()
Expand Down Expand Up @@ -196,7 +187,6 @@ class AdminControllerTest {
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
.cookie(new Cookie("token", "token")))
.andDo(print())
.andExpect(status().isOk())
.andReturn()
.getResponse()
Expand All @@ -221,7 +211,6 @@ class AdminControllerTest {
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
.cookie(new Cookie("token", "token")))
.andDo(print())
.andExpect(status().isOk());
}

Expand All @@ -232,7 +221,6 @@ class AdminControllerTest {
mockMvc.perform(delete("/admin/api/stages/{id}", 1L)
.contentType(MediaType.APPLICATION_JSON)
.cookie(new Cookie("token", "token")))
.andDo(print())
.andExpect(status().isOk());
}

Expand Down Expand Up @@ -260,7 +248,6 @@ class AdminControllerTest {
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
.cookie(new Cookie("token", "token")))
.andDo(print())
.andExpect(status().isNotFound())
.andReturn()
.getResponse()
Expand Down Expand Up @@ -295,7 +282,6 @@ class AdminControllerTest {
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
.cookie(new Cookie("token", "token")))
.andDo(print())
.andExpect(status().isOk())
.andReturn()
.getResponse()
Expand All @@ -321,7 +307,6 @@ class AdminControllerTest {
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
.cookie(new Cookie("token", "token")))
.andDo(print())
.andExpect(status().isOk())
.andReturn()
.getResponse()
Expand All @@ -341,7 +326,6 @@ class AdminControllerTest {
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
.cookie(new Cookie("token", "token")))
.andDo(print())
.andExpect(status().isBadRequest());
}

Expand All @@ -356,7 +340,6 @@ class AdminControllerTest {
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
.cookie(new Cookie("token", "token")))
.andDo(print())
.andExpect(status().isBadRequest());
}

Expand All @@ -371,7 +354,6 @@ class AdminControllerTest {
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
.cookie(new Cookie("token", "token")))
.andDo(print())
.andExpect(status().isOk());
}

Expand All @@ -386,7 +368,6 @@ class AdminControllerTest {
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
.cookie(new Cookie("token", "token")))
.andDo(print())
.andExpect(status().isBadRequest());
}

Expand All @@ -401,7 +382,6 @@ class AdminControllerTest {
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
.cookie(new Cookie("token", "token")))
.andDo(print())
.andExpect(status().isBadRequest());
}

Expand All @@ -419,7 +399,6 @@ class AdminControllerTest {
.content(objectMapper.writeValueAsString(request))
.contentType(MediaType.APPLICATION_JSON)
.cookie(new Cookie("token", "token")))
.andDo(print())
.andExpect(status().isNotFound());
}

Expand All @@ -430,7 +409,6 @@ class AdminControllerTest {
mockMvc.perform(delete("/admin/api/schools/{id}", 1L)
.contentType(MediaType.APPLICATION_JSON)
.cookie(new Cookie("token", "token")))
.andDo(print())
.andExpect(status().isOk());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,17 @@
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.web.servlet.MockMvc;

@CustomWebMvcTest(AdminViewController.class)
@CustomWebMvcTest
@DisplayNameGeneration(ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
class AdminViewControllerTest {

@Autowired
MockMvc mockMvc;

@SpyBean
@Autowired
AuthExtractor authExtractor;

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand All @@ -28,11 +27,10 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;

@CustomWebMvcTest(FestivalController.class)
@CustomWebMvcTest
@DisplayNameGeneration(ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
class FestivalControllerTest {
Expand All @@ -43,7 +41,7 @@ class FestivalControllerTest {
@Autowired
ObjectMapper objectMapper;

@MockBean
@Autowired
FestivalService festivalService;

@Captor
Expand All @@ -64,7 +62,6 @@ class FestivalControllerTest {
String content = mockMvc.perform(get("/festivals")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print())
.andReturn()
.getResponse()
.getContentAsString(StandardCharsets.UTF_8);
Expand All @@ -90,7 +87,6 @@ class FestivalControllerTest {
String content = mockMvc.perform(get("/festivals/{festivalId}", 1L)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andDo(print())
.andReturn()
.getResponse()
.getContentAsString(StandardCharsets.UTF_8);
Expand Down
Loading

0 comments on commit 3382f8a

Please sign in to comment.