diff --git a/backend/src/test/java/com/festago/acceptance/CucumberClient.java b/backend/src/test/java/com/festago/acceptance/CucumberClient.java index e0655ec16..115ba0350 100644 --- a/backend/src/test/java/com/festago/acceptance/CucumberClient.java +++ b/backend/src/test/java/com/festago/acceptance/CucumberClient.java @@ -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 { diff --git a/backend/src/test/java/com/festago/acceptance/CucumberSpringConfiguration.java b/backend/src/test/java/com/festago/acceptance/CucumberSpringConfiguration.java index 97faaca9e..3b7ec15e8 100644 --- a/backend/src/test/java/com/festago/acceptance/CucumberSpringConfiguration.java +++ b/backend/src/test/java/com/festago/acceptance/CucumberSpringConfiguration.java @@ -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; } } diff --git a/backend/src/test/java/com/festago/acceptance/DataInitializer.java b/backend/src/test/java/com/festago/acceptance/DataInitializer.java deleted file mode 100644 index 0fccf6c67..000000000 --- a/backend/src/test/java/com/festago/acceptance/DataInitializer.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.festago.acceptance; - -import java.sql.DatabaseMetaData; -import java.sql.ResultSet; -import java.util.ArrayList; -import java.util.List; -import java.util.Set; -import javax.sql.DataSource; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Profile; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.stereotype.Component; -import org.springframework.transaction.annotation.Transactional; - -@Component -@Profile("test") -public class DataInitializer implements InitializingBean { - - @Autowired - private JdbcTemplate jdbcTemplate; - - @Autowired - private DataSource dataSource; - - private List tableNames; - - private Set metaTableNames = Set.of( - "sys_config", - "flyway_schema_history", - "CONSTANTS", - "ENUM_VALUES", - "INDEXES", - "INDEX_COLUMNS", - "INFORMATION_SCHEMA_CATALOG_NAME", - "IN_DOUBT", - "LOCKS", - "QUERY_STATISTICS", - "RIGHTS", - "ROLES", - "SESSIONS", - "SESSION_STATE", - "SETTINGS", - "SYNONYMS", - "USERS" - ); - - @Override - public void afterPropertiesSet() { - tableNames = new ArrayList<>(); - try { - DatabaseMetaData metaData = dataSource.getConnection().getMetaData(); - ResultSet tables = metaData.getTables(null, null, null, new String[]{"TABLE"}); - while (tables.next()) { - String tableName = tables.getString("TABLE_NAME"); - if (metaTableNames.contains(tableName)) { - continue; - } - tableNames.add(tableName); - } - } catch (Exception e) { - throw new RuntimeException(); - } - } - - @Transactional - public void execute() { - truncateAllTables(); - } - - private void truncateAllTables() { - jdbcTemplate.execute("SET foreign_key_checks = 0;"); - tableNames.forEach( - tableName -> executeQueryWithTable(tableName) - ); - jdbcTemplate.execute("SET foreign_key_checks = 1;"); - } - - private void executeQueryWithTable(String tableName) { - jdbcTemplate.execute("TRUNCATE TABLE " + tableName); - } -} - diff --git a/backend/src/test/java/com/festago/presentation/AdminControllerTest.java b/backend/src/test/java/com/festago/presentation/AdminControllerTest.java index d4a9b6644..4a207fa04 100644 --- a/backend/src/test/java/com/festago/presentation/AdminControllerTest.java +++ b/backend/src/test/java/com/festago/presentation/AdminControllerTest.java @@ -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; @@ -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 { @@ -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 { @@ -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() @@ -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() @@ -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() @@ -221,7 +211,6 @@ class AdminControllerTest { .content(objectMapper.writeValueAsString(request)) .contentType(MediaType.APPLICATION_JSON) .cookie(new Cookie("token", "token"))) - .andDo(print()) .andExpect(status().isOk()); } @@ -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()); } @@ -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() @@ -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() @@ -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() @@ -341,7 +326,6 @@ class AdminControllerTest { .content(objectMapper.writeValueAsString(request)) .contentType(MediaType.APPLICATION_JSON) .cookie(new Cookie("token", "token"))) - .andDo(print()) .andExpect(status().isBadRequest()); } @@ -356,7 +340,6 @@ class AdminControllerTest { .content(objectMapper.writeValueAsString(request)) .contentType(MediaType.APPLICATION_JSON) .cookie(new Cookie("token", "token"))) - .andDo(print()) .andExpect(status().isBadRequest()); } @@ -371,7 +354,6 @@ class AdminControllerTest { .content(objectMapper.writeValueAsString(request)) .contentType(MediaType.APPLICATION_JSON) .cookie(new Cookie("token", "token"))) - .andDo(print()) .andExpect(status().isOk()); } @@ -386,7 +368,6 @@ class AdminControllerTest { .content(objectMapper.writeValueAsString(request)) .contentType(MediaType.APPLICATION_JSON) .cookie(new Cookie("token", "token"))) - .andDo(print()) .andExpect(status().isBadRequest()); } @@ -401,7 +382,6 @@ class AdminControllerTest { .content(objectMapper.writeValueAsString(request)) .contentType(MediaType.APPLICATION_JSON) .cookie(new Cookie("token", "token"))) - .andDo(print()) .andExpect(status().isBadRequest()); } @@ -419,7 +399,6 @@ class AdminControllerTest { .content(objectMapper.writeValueAsString(request)) .contentType(MediaType.APPLICATION_JSON) .cookie(new Cookie("token", "token"))) - .andDo(print()) .andExpect(status().isNotFound()); } @@ -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()); } } diff --git a/backend/src/test/java/com/festago/presentation/AdminViewControllerTest.java b/backend/src/test/java/com/festago/presentation/AdminViewControllerTest.java index aa48b24cf..9890a4827 100644 --- a/backend/src/test/java/com/festago/presentation/AdminViewControllerTest.java +++ b/backend/src/test/java/com/festago/presentation/AdminViewControllerTest.java @@ -16,10 +16,9 @@ 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 { @@ -27,7 +26,7 @@ class AdminViewControllerTest { @Autowired MockMvc mockMvc; - @SpyBean + @Autowired AuthExtractor authExtractor; @Test diff --git a/backend/src/test/java/com/festago/presentation/FestivalControllerTest.java b/backend/src/test/java/com/festago/presentation/FestivalControllerTest.java index 8fef4e0a3..24ed3376c 100644 --- a/backend/src/test/java/com/festago/presentation/FestivalControllerTest.java +++ b/backend/src/test/java/com/festago/presentation/FestivalControllerTest.java @@ -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; @@ -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 { @@ -43,7 +41,7 @@ class FestivalControllerTest { @Autowired ObjectMapper objectMapper; - @MockBean + @Autowired FestivalService festivalService; @Captor @@ -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); @@ -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); diff --git a/backend/src/test/java/com/festago/presentation/MemberControllerTest.java b/backend/src/test/java/com/festago/presentation/MemberControllerTest.java index 741ac7220..e7ef1d1e4 100644 --- a/backend/src/test/java/com/festago/presentation/MemberControllerTest.java +++ b/backend/src/test/java/com/festago/presentation/MemberControllerTest.java @@ -4,7 +4,6 @@ import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.BDDMockito.given; 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; @@ -18,10 +17,9 @@ 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.test.web.servlet.MockMvc; -@CustomWebMvcTest(MemberController.class) +@CustomWebMvcTest @DisplayNameGeneration(ReplaceUnderscores.class) @SuppressWarnings("NonAsciiCharacters") class MemberControllerTest { @@ -32,10 +30,10 @@ class MemberControllerTest { @Autowired ObjectMapper objectMapper; - @MockBean + @Autowired MemberService memberService; - @MockBean + @Autowired MemberTicketService memberTicketService; @Test @@ -51,7 +49,6 @@ class MemberControllerTest { String content = mockMvc.perform(get("/members/profile") .header("Authorization", "Bearer " + token)) .andExpect(status().isOk()) - .andDo(print()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); diff --git a/backend/src/test/java/com/festago/presentation/MemberTicketControllerTest.java b/backend/src/test/java/com/festago/presentation/MemberTicketControllerTest.java index 5ac7a8bdb..39fbcc72e 100644 --- a/backend/src/test/java/com/festago/presentation/MemberTicketControllerTest.java +++ b/backend/src/test/java/com/festago/presentation/MemberTicketControllerTest.java @@ -9,7 +9,6 @@ import static org.mockito.BDDMockito.given; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 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; @@ -33,12 +32,11 @@ 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.data.domain.Pageable; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; -@CustomWebMvcTest(MemberTicketController.class) +@CustomWebMvcTest @DisplayNameGeneration(ReplaceUnderscores.class) @SuppressWarnings("NonAsciiCharacters") class MemberTicketControllerTest { @@ -49,13 +47,13 @@ class MemberTicketControllerTest { @Autowired ObjectMapper objectMapper; - @MockBean + @Autowired EntryService entryService; - @MockBean + @Autowired MemberTicketService memberTicketService; - @MockBean + @Autowired TicketingService ticketingService; @Test @@ -76,7 +74,6 @@ class MemberTicketControllerTest { String content = mockMvc.perform(post("/member-tickets/{memberTicketId}/qr", memberTicketId) .contentType(MediaType.APPLICATION_JSON) .header("Authorization", "Bearer " + token)) - .andDo(print()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); @@ -106,7 +103,6 @@ class MemberTicketControllerTest { .contentType(MediaType.APPLICATION_JSON) .header("Authorization", "Bearer " + token)) .andExpect(status().isOk()) - .andDo(print()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); @@ -138,7 +134,6 @@ class MemberTicketControllerTest { .contentType(MediaType.APPLICATION_JSON) .header("Authorization", "Bearer " + token)) .andExpect(status().isOk()) - .andDo(print()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); @@ -170,7 +165,6 @@ class MemberTicketControllerTest { .contentType(MediaType.APPLICATION_JSON) .header("Authorization", "Bearer " + token)) .andExpect(status().isOk()) - .andDo(print()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); @@ -200,7 +194,6 @@ class MemberTicketControllerTest { .content(objectMapper.writeValueAsString(request)) .header("Authorization", "Bearer " + token)) .andExpect(status().isOk()) - .andDo(print()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); diff --git a/backend/src/test/java/com/festago/presentation/SchoolControllerTest.java b/backend/src/test/java/com/festago/presentation/SchoolControllerTest.java index c7cded0b3..a14bcc944 100644 --- a/backend/src/test/java/com/festago/presentation/SchoolControllerTest.java +++ b/backend/src/test/java/com/festago/presentation/SchoolControllerTest.java @@ -3,7 +3,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; 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; @@ -17,11 +16,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.http.MediaType; import org.springframework.test.web.servlet.MockMvc; -@CustomWebMvcTest(SchoolController.class) +@CustomWebMvcTest @DisplayNameGeneration(ReplaceUnderscores.class) @SuppressWarnings("NonAsciiCharacters") class SchoolControllerTest { @@ -32,7 +30,7 @@ class SchoolControllerTest { @Autowired ObjectMapper objectMapper; - @MockBean + @Autowired SchoolService schoolService; @Test @@ -51,7 +49,6 @@ class SchoolControllerTest { String content = mockMvc.perform(get("/schools") .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) - .andDo(print()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); @@ -71,7 +68,6 @@ class SchoolControllerTest { String content = mockMvc.perform(get("/schools/{id}", 1L) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) - .andDo(print()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); diff --git a/backend/src/test/java/com/festago/presentation/StaffMemberTicketControllerTest.java b/backend/src/test/java/com/festago/presentation/StaffMemberTicketControllerTest.java index 0bcdd1a26..f7b7932b7 100644 --- a/backend/src/test/java/com/festago/presentation/StaffMemberTicketControllerTest.java +++ b/backend/src/test/java/com/festago/presentation/StaffMemberTicketControllerTest.java @@ -3,7 +3,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.BDDMockito.given; 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; @@ -17,12 +16,11 @@ 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.http.MediaType; import org.springframework.test.web.servlet.MockMvc; -@CustomWebMvcTest(StaffMemberTicketController.class) +@CustomWebMvcTest @DisplayNameGeneration(ReplaceUnderscores.class) @SuppressWarnings("NonAsciiCharacters") class StaffMemberTicketControllerTest { @@ -33,7 +31,7 @@ class StaffMemberTicketControllerTest { @Autowired ObjectMapper objectMapper; - @MockBean + @Autowired EntryService entryService; @Test @@ -49,7 +47,6 @@ class StaffMemberTicketControllerTest { .content(objectMapper.writeValueAsString(request)) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) - .andDo(print()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); diff --git a/backend/src/test/java/com/festago/presentation/StageControllerTest.java b/backend/src/test/java/com/festago/presentation/StageControllerTest.java index 988c245de..a9a943646 100644 --- a/backend/src/test/java/com/festago/presentation/StageControllerTest.java +++ b/backend/src/test/java/com/festago/presentation/StageControllerTest.java @@ -4,7 +4,6 @@ import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.BDDMockito.given; 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; @@ -22,11 +21,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.http.MediaType; import org.springframework.test.web.servlet.MockMvc; -@CustomWebMvcTest(StageController.class) +@CustomWebMvcTest @DisplayNameGeneration(ReplaceUnderscores.class) @SuppressWarnings("NonAsciiCharacters") class StageControllerTest { @@ -37,10 +35,10 @@ class StageControllerTest { @Autowired ObjectMapper objectMapper; - @MockBean + @Autowired TicketService ticketService; - @MockBean + @Autowired StageService stageService; @Test @@ -59,7 +57,6 @@ class StageControllerTest { String content = mockMvc.perform(get("/stages/{stageId}/tickets", 1L) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) - .andDo(print()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); @@ -78,7 +75,6 @@ class StageControllerTest { String content = mockMvc.perform(get("/stages/{stageId}", 1L) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) - .andDo(print()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); diff --git a/backend/src/test/java/com/festago/presentation/StudentControllerTest.java b/backend/src/test/java/com/festago/presentation/StudentControllerTest.java index 8dfd17dae..07b1bac77 100644 --- a/backend/src/test/java/com/festago/presentation/StudentControllerTest.java +++ b/backend/src/test/java/com/festago/presentation/StudentControllerTest.java @@ -5,7 +5,6 @@ import static org.mockito.BDDMockito.given; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; 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; @@ -22,14 +21,13 @@ 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.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.test.web.servlet.MockMvc; +@CustomWebMvcTest @DisplayNameGeneration(ReplaceUnderscores.class) @SuppressWarnings("NonAsciiCharacters") -@CustomWebMvcTest(StudentController.class) class StudentControllerTest { @Autowired @@ -38,7 +36,7 @@ class StudentControllerTest { @Autowired ObjectMapper objectMapper; - @MockBean + @Autowired StudentService studentService; @Nested @@ -131,7 +129,6 @@ class 학생_인증_정보_조회 { .header(HttpHeaders.AUTHORIZATION, "Bearer token") ) .andExpect(status().isOk()) - .andDo(print()) .andReturn() .getResponse() .getContentAsString(StandardCharsets.UTF_8); diff --git a/backend/src/test/java/com/festago/auth/presentation/AuthControllerTest.java b/backend/src/test/java/com/festago/presentation/auth/AuthControllerTest.java similarity index 93% rename from backend/src/test/java/com/festago/auth/presentation/AuthControllerTest.java rename to backend/src/test/java/com/festago/presentation/auth/AuthControllerTest.java index 6b9cf1786..bcf04c7f9 100644 --- a/backend/src/test/java/com/festago/auth/presentation/AuthControllerTest.java +++ b/backend/src/test/java/com/festago/presentation/auth/AuthControllerTest.java @@ -1,4 +1,4 @@ -package com.festago.auth.presentation; +package com.festago.presentation.auth; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; @@ -7,7 +7,6 @@ import static org.mockito.Mockito.verify; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; 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; @@ -17,18 +16,16 @@ import com.festago.auth.dto.LoginRequest; import com.festago.auth.dto.LoginResponse; import com.festago.fcm.application.MemberFCMService; -import com.festago.presentation.AuthController; import com.festago.support.CustomWebMvcTest; import com.festago.support.WithMockAuth; import org.junit.jupiter.api.DisplayNameGeneration; 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.http.MediaType; import org.springframework.test.web.servlet.MockMvc; -@CustomWebMvcTest(AuthController.class) +@CustomWebMvcTest @DisplayNameGeneration(ReplaceUnderscores.class) @SuppressWarnings("NonAsciiCharacters") class AuthControllerTest { @@ -39,10 +36,10 @@ class AuthControllerTest { @Autowired ObjectMapper objectMapper; - @MockBean + @Autowired AuthFacadeService authFacadeService; - @MockBean + @Autowired MemberFCMService memberFCMService; @Test @@ -58,7 +55,6 @@ class AuthControllerTest { .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(request))) .andExpect(status().isOk()) - .andDo(print()) .andReturn() .getResponse() .getContentAsString(); @@ -108,7 +104,6 @@ class AuthControllerTest { .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(request))) .andExpect(status().isOk()) - .andDo(print()) .andReturn() .getResponse() .getContentAsString(); @@ -135,7 +130,6 @@ class AuthControllerTest { .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(request))) .andExpect(status().isOk()) - .andDo(print()) .andReturn() .getResponse() .getContentAsString(); diff --git a/backend/src/test/java/com/festago/auth/presentation/RoleArgumentResolverTest.java b/backend/src/test/java/com/festago/presentation/auth/RoleArgumentResolverTest.java similarity index 94% rename from backend/src/test/java/com/festago/auth/presentation/RoleArgumentResolverTest.java rename to backend/src/test/java/com/festago/presentation/auth/RoleArgumentResolverTest.java index c1b1b2514..4b90cc471 100644 --- a/backend/src/test/java/com/festago/auth/presentation/RoleArgumentResolverTest.java +++ b/backend/src/test/java/com/festago/presentation/auth/RoleArgumentResolverTest.java @@ -1,4 +1,4 @@ -package com.festago.auth.presentation; +package com.festago.presentation.auth; import static com.festago.common.exception.ErrorCode.NOT_ENOUGH_PERMISSION; import static org.assertj.core.api.Assertions.assertThat; @@ -6,8 +6,6 @@ import com.festago.auth.domain.Role; import com.festago.common.exception.ForbiddenException; -import com.festago.presentation.auth.AuthenticateContext; -import com.festago.presentation.auth.RoleArgumentResolver; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores; diff --git a/backend/src/test/java/com/festago/support/ApplicationIntegrationTest.java b/backend/src/test/java/com/festago/support/ApplicationIntegrationTest.java index 6bd69a561..f76220f21 100644 --- a/backend/src/test/java/com/festago/support/ApplicationIntegrationTest.java +++ b/backend/src/test/java/com/festago/support/ApplicationIntegrationTest.java @@ -1,11 +1,15 @@ package com.festago.support; -import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.TestExecutionListeners; -@SpringBootTest(webEnvironment = WebEnvironment.NONE) -@ExtendWith(DatabaseClearExtension.class) +@SpringBootTest +@TestExecutionListeners(value = { + ResetMockTestExecutionListener.class, + DatabaseClearTestExecutionListener.class +}, mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS) +@Import({TestTimeConfig.class}) public abstract class ApplicationIntegrationTest { } diff --git a/backend/src/test/java/com/festago/support/CustomWebMvcTest.java b/backend/src/test/java/com/festago/support/CustomWebMvcTest.java index c19dde866..897786c8e 100644 --- a/backend/src/test/java/com/festago/support/CustomWebMvcTest.java +++ b/backend/src/test/java/com/festago/support/CustomWebMvcTest.java @@ -5,19 +5,15 @@ import java.lang.annotation.RetentionPolicy; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; import org.springframework.context.annotation.Import; -import org.springframework.core.annotation.AliasFor; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.TestExecutionListeners.MergeMode; @WebMvcTest -@Import({TestAuthConfig.class, ErrorLoggerConfig.class}) +@Import({TestAuthConfig.class, ErrorLoggerConfig.class, MockAllServiceConfig.class}) @Retention(RetentionPolicy.RUNTIME) -@TestExecutionListeners(value = MockAuthTestExecutionListener.class, mergeMode = MergeMode.MERGE_WITH_DEFAULTS) +@TestExecutionListeners(value = {MockAuthTestExecutionListener.class, + ResetMockTestExecutionListener.class}, mergeMode = MergeMode.MERGE_WITH_DEFAULTS) public @interface CustomWebMvcTest { - @AliasFor("controllers") Class[] value() default {}; - - @AliasFor("value") - Class[] controllers() default {}; } diff --git a/backend/src/test/java/com/festago/support/DatabaseClearExtension.java b/backend/src/test/java/com/festago/support/DatabaseClearExtension.java deleted file mode 100644 index b2f9b1e3c..000000000 --- a/backend/src/test/java/com/festago/support/DatabaseClearExtension.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.festago.support; - -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import org.junit.jupiter.api.extension.AfterAllCallback; -import org.junit.jupiter.api.extension.AfterEachCallback; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.api.extension.ExtensionContext.Namespace; -import org.junit.jupiter.api.extension.TestInstanceFactoryContext; -import org.junit.jupiter.api.extension.TestInstancePreConstructCallback; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.test.context.junit.jupiter.SpringExtension; - -public class DatabaseClearExtension implements TestInstancePreConstructCallback, AfterEachCallback, AfterAllCallback { - - private static final Namespace CLEANER = Namespace.create("festago.database.cleaner"); - private static final Set TABLES = new HashSet<>(); - private static final String JDBC_TEMPLATE = "jdbcTemplate"; - - @Override - public void preConstructTestInstance(TestInstanceFactoryContext factoryContext, ExtensionContext context) - throws Exception { - JdbcTemplate jdbcTemplate = SpringExtension.getApplicationContext(context) - .getBean(JdbcTemplate.class); - context.getStore(CLEANER).put(JDBC_TEMPLATE, jdbcTemplate); - initialTable(context); - truncate(context); - } - - private void initialTable(ExtensionContext context) { - if (TABLES.isEmpty()) { - JdbcTemplate jdbcTemplate = context.getStore(CLEANER).get(JDBC_TEMPLATE, JdbcTemplate.class); - List tables = jdbcTemplate.query("SHOW TABLES", (rs, rowNum) -> rs.getString(1)); - TABLES.addAll(tables); - } - } - - private void truncate(ExtensionContext context) { - JdbcTemplate jdbcTemplate = context.getStore(CLEANER).get(JDBC_TEMPLATE, JdbcTemplate.class); - jdbcTemplate.update("SET FOREIGN_KEY_CHECKS = ?", 0); - for (String table : TABLES) { - jdbcTemplate.update("TRUNCATE TABLE " + table); - } - jdbcTemplate.update("SET FOREIGN_KEY_CHECKS = ?", 1); - } - - @Override - public void afterEach(ExtensionContext context) throws Exception { - truncate(context); - } - - @Override - public void afterAll(ExtensionContext context) throws Exception { - context.getStore(CLEANER).remove(JDBC_TEMPLATE); - } -} diff --git a/backend/src/test/java/com/festago/support/DatabaseClearTestExecutionListener.java b/backend/src/test/java/com/festago/support/DatabaseClearTestExecutionListener.java new file mode 100644 index 000000000..ba28ab463 --- /dev/null +++ b/backend/src/test/java/com/festago/support/DatabaseClearTestExecutionListener.java @@ -0,0 +1,49 @@ +package com.festago.support; + +import java.util.HashSet; +import java.util.Set; +import org.springframework.context.ApplicationContext; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.test.context.TestContext; +import org.springframework.test.context.TestExecutionListener; + +public class DatabaseClearTestExecutionListener implements TestExecutionListener { + + private static final Set tables = new HashSet<>(); + private static final ThreadLocal jdbcTemplates = new ThreadLocal<>(); + + @Override + public void prepareTestInstance(TestContext testContext) throws Exception { + ApplicationContext ac = testContext.getApplicationContext(); + JdbcTemplate jdbcTemplate = ac.getBean(JdbcTemplate.class); + jdbcTemplates.set(jdbcTemplate); + initialTable(); + truncate(); + } + + private void initialTable() { + if (tables.isEmpty()) { + JdbcTemplate jdbcTemplate = jdbcTemplates.get(); + tables.addAll(jdbcTemplate.query("SHOW TABLES", (rs, rowNum) -> rs.getString(1))); + } + } + + private void truncate() { + JdbcTemplate jdbcTemplate = jdbcTemplates.get(); + jdbcTemplate.update("SET FOREIGN_KEY_CHECKS = ?", 0); + for (String table : tables) { + jdbcTemplate.update("TRUNCATE TABLE " + table); + } + jdbcTemplate.update("SET FOREIGN_KEY_CHECKS = ?", 1); + } + + @Override + public void afterTestMethod(TestContext testContext) throws Exception { + truncate(); + } + + @Override + public void afterTestClass(TestContext testContext) throws Exception { + jdbcTemplates.remove(); + } +} diff --git a/backend/src/test/java/com/festago/support/MockAllServiceConfig.java b/backend/src/test/java/com/festago/support/MockAllServiceConfig.java new file mode 100644 index 000000000..4d4ba3a7b --- /dev/null +++ b/backend/src/test/java/com/festago/support/MockAllServiceConfig.java @@ -0,0 +1,81 @@ +package com.festago.support; + +import com.festago.auth.application.AdminAuthService; +import com.festago.auth.application.AuthFacadeService; +import com.festago.entry.application.EntryService; +import com.festago.fcm.application.MemberFCMService; +import com.festago.festival.application.FestivalService; +import com.festago.member.application.MemberService; +import com.festago.school.application.SchoolService; +import com.festago.stage.application.StageService; +import com.festago.student.application.StudentService; +import com.festago.ticket.application.TicketService; +import com.festago.ticketing.application.MemberTicketService; +import com.festago.ticketing.application.TicketingService; +import org.mockito.Mockito; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; + +@TestConfiguration +public class MockAllServiceConfig { + + @Bean + public FestivalService festivalService() { + return Mockito.mock(FestivalService.class); + } + + @Bean + public StageService stageService() { + return Mockito.mock(StageService.class); + } + + @Bean + public TicketService ticketService() { + return Mockito.mock(TicketService.class); + } + + @Bean + public AdminAuthService adminAuthService() { + return Mockito.mock(AdminAuthService.class); + } + + @Bean + public SchoolService schoolService() { + return Mockito.mock(SchoolService.class); + } + + @Bean + public MemberTicketService memberTicketService() { + return Mockito.mock(MemberTicketService.class); + } + + @Bean + public EntryService entryService() { + return Mockito.mock(EntryService.class); + } + + @Bean + public TicketingService ticketingService() { + return Mockito.mock(TicketingService.class); + } + + @Bean + public AuthFacadeService authFacadeService() { + return Mockito.mock(AuthFacadeService.class); + } + + @Bean + public MemberFCMService memberFCMService() { + return Mockito.mock(MemberFCMService.class); + } + + @Bean + public MemberService memberService() { + return Mockito.mock(MemberService.class); + } + + @Bean + public StudentService studentService() { + return Mockito.mock(StudentService.class); + } +} diff --git a/backend/src/test/java/com/festago/support/MockAuthTestExecutionListener.java b/backend/src/test/java/com/festago/support/MockAuthTestExecutionListener.java index 5cf14879c..ef48f0d07 100644 --- a/backend/src/test/java/com/festago/support/MockAuthTestExecutionListener.java +++ b/backend/src/test/java/com/festago/support/MockAuthTestExecutionListener.java @@ -5,9 +5,9 @@ import java.lang.reflect.Method; import org.springframework.context.ApplicationContext; import org.springframework.test.context.TestContext; -import org.springframework.test.context.support.AbstractTestExecutionListener; +import org.springframework.test.context.TestExecutionListener; -public class MockAuthTestExecutionListener extends AbstractTestExecutionListener { +public class MockAuthTestExecutionListener implements TestExecutionListener { @Override public void beforeTestMethod(TestContext testContext) throws Exception { diff --git a/backend/src/test/java/com/festago/support/RepositoryTest.java b/backend/src/test/java/com/festago/support/RepositoryTest.java index e4b2414d5..acc7b28f6 100644 --- a/backend/src/test/java/com/festago/support/RepositoryTest.java +++ b/backend/src/test/java/com/festago/support/RepositoryTest.java @@ -1,14 +1,18 @@ package com.festago.support; +import com.festago.config.JpaAuditingConfig; import com.festago.config.QuerydslConfig; +import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.context.annotation.Import; +@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) -@DataJpaTest -@Import(QuerydslConfig.class) +@DataJpaTest(showSql = false) +@Import({QuerydslConfig.class, JpaAuditingConfig.class}) public @interface RepositoryTest { } diff --git a/backend/src/test/java/com/festago/support/ResetMockTestExecutionListener.java b/backend/src/test/java/com/festago/support/ResetMockTestExecutionListener.java new file mode 100644 index 000000000..d68705664 --- /dev/null +++ b/backend/src/test/java/com/festago/support/ResetMockTestExecutionListener.java @@ -0,0 +1,22 @@ +package com.festago.support; + +import org.mockito.MockingDetails; +import org.mockito.Mockito; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.TestContext; +import org.springframework.test.context.TestExecutionListener; + +public class ResetMockTestExecutionListener implements TestExecutionListener { + + @Override + public void afterTestMethod(TestContext testContext) throws Exception { + ApplicationContext applicationContext = testContext.getApplicationContext(); + for (String beanName : applicationContext.getBeanDefinitionNames()) { + Object bean = applicationContext.getBean(beanName); + MockingDetails mockingDetails = Mockito.mockingDetails(bean); + if (mockingDetails.isMock()) { + Mockito.reset(bean); + } + } + } +} diff --git a/backend/src/test/java/com/festago/support/TestAuthConfig.java b/backend/src/test/java/com/festago/support/TestAuthConfig.java index c0a384dee..d1d41deeb 100644 --- a/backend/src/test/java/com/festago/support/TestAuthConfig.java +++ b/backend/src/test/java/com/festago/support/TestAuthConfig.java @@ -2,6 +2,7 @@ import com.festago.auth.application.AuthExtractor; import com.festago.presentation.auth.AuthenticateContext; +import org.mockito.Mockito; import org.springframework.boot.test.context.TestConfiguration; import org.springframework.context.annotation.Bean; @@ -15,6 +16,6 @@ public AuthenticateContext authenticateContext() { @Bean public AuthExtractor authExtractor(AuthenticateContext authenticateContext) { - return new MockAuthExtractor(authenticateContext); + return Mockito.spy(new MockAuthExtractor(authenticateContext)); } } diff --git a/backend/src/test/java/com/festago/support/TestTimeConfig.java b/backend/src/test/java/com/festago/support/TestTimeConfig.java new file mode 100644 index 000000000..a273907ae --- /dev/null +++ b/backend/src/test/java/com/festago/support/TestTimeConfig.java @@ -0,0 +1,17 @@ +package com.festago.support; + +import java.time.Clock; +import org.mockito.Mockito; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Primary; + +@TestConfiguration +public class TestTimeConfig { + + @Bean("testClock") + @Primary + public Clock clock() { + return Mockito.spy(Clock.systemDefaultZone()); + } +} diff --git a/backend/src/test/java/com/festago/ticket/application/integration/TicketServiceIntegrationTest.java b/backend/src/test/java/com/festago/ticket/application/integration/TicketServiceIntegrationTest.java index b77516f87..9e119af49 100644 --- a/backend/src/test/java/com/festago/ticket/application/integration/TicketServiceIntegrationTest.java +++ b/backend/src/test/java/com/festago/ticket/application/integration/TicketServiceIntegrationTest.java @@ -30,7 +30,6 @@ 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; @DisplayNameGeneration(ReplaceUnderscores.class) @SuppressWarnings("NonAsciiCharacters") @@ -54,7 +53,7 @@ class TicketServiceIntegrationTest extends ApplicationIntegrationTest { @Autowired SchoolRepository schoolRepository; - @SpyBean + @Autowired Clock clock; @Test @@ -78,8 +77,7 @@ class TicketServiceIntegrationTest extends ApplicationIntegrationTest { // given LocalDateTime stageStartTime = LocalDateTime.parse("2022-07-26T18:00:00"); doReturn(stageStartTime.minusWeeks(1).toInstant(ZoneOffset.UTC)) - .when(clock) - .instant(); + .when(clock).instant(); School school = schoolRepository.save(SchoolFixture.school().build()); Festival festival = festivalRepository.save(FestivalFixture.festival() .school(school) @@ -107,8 +105,7 @@ class TicketServiceIntegrationTest extends ApplicationIntegrationTest { // given LocalDateTime stageStartTime = LocalDateTime.parse("2022-07-26T18:00:00"); doReturn(stageStartTime.minusWeeks(1).toInstant(ZoneOffset.UTC)) - .when(clock) - .instant(); + .when(clock).instant(); School school = schoolRepository.save(SchoolFixture.school().build()); Festival festival = festivalRepository.save(FestivalFixture.festival() .school(school) diff --git a/backend/src/test/java/com/festago/ticketing/repository/MemberTicketRepositoryTest.java b/backend/src/test/java/com/festago/ticketing/repository/MemberTicketRepositoryTest.java index c4d7beb56..3472e2269 100644 --- a/backend/src/test/java/com/festago/ticketing/repository/MemberTicketRepositoryTest.java +++ b/backend/src/test/java/com/festago/ticketing/repository/MemberTicketRepositoryTest.java @@ -3,7 +3,6 @@ import static org.assertj.core.api.Assertions.assertThat; import com.festago.common.domain.BaseTimeEntity; -import com.festago.config.JpaAuditingConfig; import com.festago.festival.domain.Festival; import com.festago.festival.repository.FestivalRepository; import com.festago.member.domain.Member; @@ -28,12 +27,10 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Import; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; -@Import(JpaAuditingConfig.class) @DisplayNameGeneration(ReplaceUnderscores.class) @SuppressWarnings("NonAsciiCharacters") @RepositoryTest