diff --git a/build.gradle b/build.gradle index f719980b..982311e2 100644 --- a/build.gradle +++ b/build.gradle @@ -177,8 +177,8 @@ dependencies { implementation 'com.github.maricn:logback-slack-appender:1.6.1' compileOnly 'org.projectlombok:lombok' -// runtimeOnly 'com.mysql:mysql-connector-j' - runtimeOnly 'org.postgresql:postgresql' // DB 변경 + runtimeOnly 'org.postgresql:postgresql' + runtimeOnly 'com.h2database:h2' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' diff --git a/src/main/java/com/map/gaja/group/infrastructure/GroupQueryRepository.java b/src/main/java/com/map/gaja/group/infrastructure/GroupQueryRepository.java index a2f59d33..e9f666b9 100644 --- a/src/main/java/com/map/gaja/group/infrastructure/GroupQueryRepository.java +++ b/src/main/java/com/map/gaja/group/infrastructure/GroupQueryRepository.java @@ -5,8 +5,10 @@ import com.querydsl.core.types.Projections; import com.querydsl.jpa.impl.JPAQueryFactory; import lombok.RequiredArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; +import javax.persistence.EntityManager; import java.util.List; import java.util.Optional; @@ -16,6 +18,8 @@ @Repository @RequiredArgsConstructor public class GroupQueryRepository { + @Autowired + EntityManager em; private final JPAQueryFactory query; /** diff --git a/src/test/java/com/map/gaja/client/infrastructure/repository/ClientBulkNativeRepositoryTest.java b/src/test/java/com/map/gaja/client/infrastructure/repository/ClientBulkNativeRepositoryTest.java index 2cf36d58..bf06bfda 100644 --- a/src/test/java/com/map/gaja/client/infrastructure/repository/ClientBulkNativeRepositoryTest.java +++ b/src/test/java/com/map/gaja/client/infrastructure/repository/ClientBulkNativeRepositoryTest.java @@ -2,22 +2,19 @@ import com.map.gaja.TestEntityCreator; import com.map.gaja.client.domain.model.Client; +import com.map.gaja.common.NativeRepositoryTest; import com.map.gaja.group.domain.model.Group; import com.map.gaja.user.domain.model.User; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.BeforeEach; 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; import javax.persistence.EntityManager; import java.util.ArrayList; import java.util.List; -@SpringBootTest -@Transactional -class ClientBulkNativeRepositoryTest { +class ClientBulkNativeRepositoryTest extends NativeRepositoryTest { @Autowired ClientBulkRepository repository; diff --git a/src/test/java/com/map/gaja/client/infrastructure/repository/ClientImageRepositoryTest.java b/src/test/java/com/map/gaja/client/infrastructure/repository/ClientImageRepositoryTest.java index 96a63702..f2702416 100644 --- a/src/test/java/com/map/gaja/client/infrastructure/repository/ClientImageRepositoryTest.java +++ b/src/test/java/com/map/gaja/client/infrastructure/repository/ClientImageRepositoryTest.java @@ -8,8 +8,6 @@ //import org.junit.jupiter.api.DisplayName; //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; // //import javax.persistence.EntityManager; // @@ -17,9 +15,7 @@ // //import static org.junit.jupiter.api.Assertions.*; // -//@SpringBootTest -//@Transactional -//class ClientImageRepositoryTest { +//class ClientImageRepositoryTest extends NativeRepositoryTest{ // @Autowired // ClientImageRepository clientImageRepository; // diff --git a/src/test/java/com/map/gaja/client/infrastructure/repository/ClientNativeRepositoryTest.java b/src/test/java/com/map/gaja/client/infrastructure/repository/ClientNativeRepositoryTest.java index 87aba6c0..24be3a25 100644 --- a/src/test/java/com/map/gaja/client/infrastructure/repository/ClientNativeRepositoryTest.java +++ b/src/test/java/com/map/gaja/client/infrastructure/repository/ClientNativeRepositoryTest.java @@ -1,6 +1,7 @@ package com.map.gaja.client.infrastructure.repository; import com.map.gaja.client.domain.model.Client; +import com.map.gaja.common.NativeRepositoryTest; import com.map.gaja.global.event.Events; import com.map.gaja.group.domain.model.Group; import com.map.gaja.user.domain.model.Authority; @@ -10,9 +11,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationEventPublisher; -import org.springframework.transaction.annotation.Transactional; import javax.persistence.EntityManager; @@ -20,9 +19,7 @@ import static org.junit.jupiter.api.Assertions.*; -@SpringBootTest -@Transactional -class ClientNativeRepositoryTest { +class ClientNativeRepositoryTest extends NativeRepositoryTest { @Autowired ClientRepository clientRepository; diff --git a/src/test/java/com/map/gaja/client/infrastructure/repository/ClientQueryNativeRepositoryTest.java b/src/test/java/com/map/gaja/client/infrastructure/repository/ClientQueryNativeRepositoryTest.java index 1222ceff..1f2d852b 100644 --- a/src/test/java/com/map/gaja/client/infrastructure/repository/ClientQueryNativeRepositoryTest.java +++ b/src/test/java/com/map/gaja/client/infrastructure/repository/ClientQueryNativeRepositoryTest.java @@ -1,6 +1,7 @@ package com.map.gaja.client.infrastructure.repository; import com.map.gaja.TestEntityCreator; +import com.map.gaja.common.NativeRepositoryTest; import com.map.gaja.group.domain.model.Group; import com.map.gaja.client.domain.model.Client; import com.map.gaja.client.presentation.dto.request.NearbyClientSearchRequest; @@ -11,8 +12,6 @@ import org.junit.jupiter.api.DisplayName; 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; import javax.persistence.EntityManager; import java.util.ArrayList; @@ -20,9 +19,7 @@ import static org.assertj.core.api.Assertions.*; -@SpringBootTest -@Transactional -class ClientQueryNativeRepositoryTest { +class ClientQueryNativeRepositoryTest extends NativeRepositoryTest { @Autowired ClientQueryRepository clientQueryRepository; diff --git a/src/test/java/com/map/gaja/client/infrastructure/repository/ClientRepositoryTest.java b/src/test/java/com/map/gaja/client/infrastructure/repository/ClientRepositoryTest.java index 886ad0e8..27554ccd 100644 --- a/src/test/java/com/map/gaja/client/infrastructure/repository/ClientRepositoryTest.java +++ b/src/test/java/com/map/gaja/client/infrastructure/repository/ClientRepositoryTest.java @@ -2,6 +2,7 @@ import com.map.gaja.TestEntityCreator; import com.map.gaja.client.domain.model.Client; +import com.map.gaja.common.RepositoryTest; import com.map.gaja.global.event.Events; import com.map.gaja.group.domain.model.Group; import com.map.gaja.user.domain.model.User; @@ -18,9 +19,7 @@ import static org.assertj.core.api.Assertions.assertThat; -@SpringBootTest -@Transactional -class ClientRepositoryTest { +class ClientRepositoryTest extends RepositoryTest { @Autowired ClientRepository clientRepository; diff --git a/src/test/java/com/map/gaja/client/presentation/api/ClientDeleteControllerTest.java b/src/test/java/com/map/gaja/client/presentation/api/ClientDeleteControllerTest.java index f8bebda1..5b40edac 100644 --- a/src/test/java/com/map/gaja/client/presentation/api/ClientDeleteControllerTest.java +++ b/src/test/java/com/map/gaja/client/presentation/api/ClientDeleteControllerTest.java @@ -1,20 +1,12 @@ package com.map.gaja.client.presentation.api; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.map.gaja.client.application.*; import com.map.gaja.client.presentation.dto.request.ClientIdsRequest; -import com.map.gaja.global.authentication.AuthenticationRepository; +import com.map.gaja.common.ControllerTest; import com.map.gaja.global.authentication.PrincipalDetails; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; 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.data.jpa.mapping.JpaMetamodelMappingContext; import org.springframework.http.MediaType; import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; -import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; @@ -25,30 +17,9 @@ import static org.mockito.Mockito.*; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; -@WebMvcTest(ClientDeleteController.class) -@MockBean(JpaMetamodelMappingContext.class) -class ClientDeleteControllerTest { - - @Autowired - MockMvc mvc; - - @MockBean - ClientBulkService clientBulkService; - @MockBean - ClientDeleteService clientDeleteService; - @MockBean - ClientAccessVerifyService clientAccessVerifyService; - @MockBean - AuthenticationRepository authenticationRepository; - +class ClientDeleteControllerTest extends ControllerTest { Long groupId = 1L; Long clientId = 1L; - private ObjectMapper om; - - @BeforeEach - void beforeEach() { - om = new ObjectMapper(); - } @Test @DisplayName("고객 삭제 성공") @@ -69,7 +40,7 @@ void deleteBulkClientTest() throws Exception { String testUrl = "/api/group/{groupId}/clients/bulk-delete"; List clientIds = List.of(1L, 2L, 3L); ClientIdsRequest clientIdsRequest = new ClientIdsRequest(clientIds); - String jsonBody = om.writeValueAsString(clientIdsRequest); + String jsonBody = mapper.writeValueAsString(clientIdsRequest); MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post(testUrl, groupId) .with(csrf()) diff --git a/src/test/java/com/map/gaja/client/presentation/api/ClientSavingControllerTest.java b/src/test/java/com/map/gaja/client/presentation/api/ClientSavingControllerTest.java index 76fa35a9..fbfba678 100644 --- a/src/test/java/com/map/gaja/client/presentation/api/ClientSavingControllerTest.java +++ b/src/test/java/com/map/gaja/client/presentation/api/ClientSavingControllerTest.java @@ -1,29 +1,17 @@ package com.map.gaja.client.presentation.api; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.map.gaja.client.application.*; -import com.map.gaja.client.domain.exception.InvalidFileException; -import com.map.gaja.client.infrastructure.s3.S3FileService; -import com.map.gaja.client.application.validator.ClientRequestValidator; import com.map.gaja.client.presentation.dto.request.NewClientRequest; import com.map.gaja.client.presentation.dto.request.simple.SimpleClientBulkRequest; import com.map.gaja.client.presentation.dto.request.simple.SimpleNewClientRequest; import com.map.gaja.client.presentation.dto.response.ClientOverviewResponse; import com.map.gaja.client.presentation.dto.subdto.StoredFileDto; -import com.map.gaja.global.authentication.AuthenticationRepository; +import com.map.gaja.common.ControllerTest; import com.map.gaja.global.authentication.PrincipalDetails; -import com.map.gaja.group.application.GroupAccessVerifyService; import com.map.gaja.group.domain.exception.GroupNotFoundException; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; 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.data.jpa.mapping.JpaMetamodelMappingContext; import org.springframework.http.MediaType; import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; -import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; @@ -35,33 +23,9 @@ import static org.mockito.Mockito.times; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; -@WebMvcTest(ClientSavingController.class) -@MockBean(JpaMetamodelMappingContext.class) -public class ClientSavingControllerTest { - @Autowired - MockMvc mvc; - - @MockBean - ClientBulkService clientBulkService; - @MockBean - GroupAccessVerifyService groupAccessVerifyService; - @MockBean - ClientRequestValidator clientRequestValidator; - @MockBean - AuthenticationRepository authenticationRepository; - @MockBean - ClientSavingService clientSavingService; - - private ObjectMapper om; - +public class ClientSavingControllerTest extends ControllerTest { static Long groupId = 1L; - static Long clientId = 1L; - - @BeforeEach - void beforeEach() { - om = new ObjectMapper(); - } @Test @DisplayName("이미지 없이 고객 등록") @@ -117,7 +81,7 @@ void addSimpleBulkClient() throws Exception { ); SimpleClientBulkRequest request = new SimpleClientBulkRequest(groupId, clientsRequest); - String jsonBody = om.writeValueAsString(request); + String jsonBody = mapper.writeValueAsString(request); MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.post(testUrl, groupId) .with(csrf()) diff --git a/src/test/java/com/map/gaja/client/presentation/api/ClientUpdatingControllerTest.java b/src/test/java/com/map/gaja/client/presentation/api/ClientUpdatingControllerTest.java index 450dbda6..96bb3537 100644 --- a/src/test/java/com/map/gaja/client/presentation/api/ClientUpdatingControllerTest.java +++ b/src/test/java/com/map/gaja/client/presentation/api/ClientUpdatingControllerTest.java @@ -1,23 +1,13 @@ package com.map.gaja.client.presentation.api; import com.fasterxml.jackson.databind.ObjectMapper; -import com.map.gaja.client.application.*; import com.map.gaja.client.domain.exception.ClientNotFoundException; -import com.map.gaja.client.domain.exception.InvalidFileException; -import com.map.gaja.client.infrastructure.s3.S3FileService; -import com.map.gaja.client.application.validator.ClientRequestValidator; import com.map.gaja.client.presentation.dto.request.NewClientRequest; import com.map.gaja.client.presentation.dto.response.ClientOverviewResponse; import com.map.gaja.client.presentation.dto.subdto.StoredFileDto; -import com.map.gaja.global.authentication.AuthenticationRepository; -import com.map.gaja.group.application.GroupAccessVerifyService; +import com.map.gaja.common.ControllerTest; import org.junit.jupiter.api.DisplayName; 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.data.jpa.mapping.JpaMetamodelMappingContext; -import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; @@ -25,25 +15,7 @@ import static org.mockito.Mockito.*; import static org.mockito.Mockito.when; -@WebMvcTest(ClientUpdatingController.class) -@MockBean(JpaMetamodelMappingContext.class) -public class ClientUpdatingControllerTest { - @Autowired - MockMvc mvc; - - @MockBean - ClientUpdatingService clientUpdatingService; - @MockBean - ClientAccessVerifyService clientAccessVerifyService; - @MockBean - GroupAccessVerifyService groupAccessVerifyService; - @MockBean - ClientRequestValidator clientRequestValidator; - @MockBean - AuthenticationRepository authenticationRepository; - - - +public class ClientUpdatingControllerTest extends ControllerTest { final String testUri = "/api/group/{groupId}/clients/{clientId}"; diff --git a/src/test/java/com/map/gaja/client/presentation/web/WebClientControllerTest.java b/src/test/java/com/map/gaja/client/presentation/web/WebClientControllerTest.java index 6ca689e9..1c11024f 100644 --- a/src/test/java/com/map/gaja/client/presentation/web/WebClientControllerTest.java +++ b/src/test/java/com/map/gaja/client/presentation/web/WebClientControllerTest.java @@ -1,31 +1,21 @@ package com.map.gaja.client.presentation.web; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.map.gaja.client.application.ClientBulkService; -import com.map.gaja.client.application.ClientUpdatingService; -import com.map.gaja.client.infrastructure.file.FileParsingService; -import com.map.gaja.client.infrastructure.file.FileValidator; import com.map.gaja.client.infrastructure.file.parser.dto.ParsedClientDto; import com.map.gaja.client.presentation.dto.request.subdto.LocationDto; import com.map.gaja.client.presentation.dto.response.InvalidExcelDataResponse; -import com.map.gaja.global.authentication.AuthenticationRepository; +import com.map.gaja.common.ControllerTest; + import com.map.gaja.global.authentication.PrincipalDetails; -import com.map.gaja.group.application.GroupAccessVerifyService; -import com.map.gaja.group.application.GroupService; -import com.map.gaja.client.infrastructure.geocode.Geocoder; import com.map.gaja.user.domain.model.Authority; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; 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.data.jpa.mapping.JpaMetamodelMappingContext; + import org.springframework.http.MediaType; import org.springframework.mock.web.MockMultipartFile; import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; -import org.springframework.test.web.servlet.MockMvc; + import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; @@ -42,42 +32,9 @@ import static org.mockito.Mockito.*; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; -@WebMvcTest(WebClientController.class) -@MockBean(JpaMetamodelMappingContext.class) -class WebClientControllerTest { +class WebClientControllerTest extends ControllerTest { private final String excelFilePath = "src/test/resources/static/file/sample-success.xlsx"; private final String testUrl = "/api/clients/file"; - - @Autowired - MockMvc mvc; - - @MockBean - FileParsingService parsingService; - - @MockBean - GroupAccessVerifyService groupAccessVerifyService; - - @MockBean - ClientUpdatingService clientUpdatingService; - - @MockBean - ClientBulkService clientBulkService; - - @MockBean - GroupService groupService; - - @MockBean - Geocoder geocoder; - - @MockBean - FileValidator fileValidator; - - @MockBean - AuthenticationRepository userGetter; - - @Autowired - ObjectMapper om; - Long groupId = 1L; @BeforeEach @@ -95,7 +52,7 @@ void parsingSuccessTest() throws Exception { int successSize = successList.size(); when(parsingService.parseClientFile(any())).thenReturn(successList); when(geocoder.convertToCoordinatesAsync(successList)).thenReturn(Mono.empty()); - when(userGetter.getAuthority()).thenReturn(List.of(Authority.FREE)); + when(authenticationRepository.getAuthority()).thenReturn(List.of(Authority.FREE)); /* doAnswer(invocation -> { List data = invocation.getArgument(0); @@ -128,7 +85,7 @@ void parsingFailTest() throws Exception { failList.add(createInvalidClientExcelData(failIdx1)); failList.add(createInvalidClientExcelData(failIdx2)); when(parsingService.parseClientFile(any())).thenReturn(failList); - when(userGetter.getAuthority()).thenReturn(List.of(Authority.FREE)); + when(authenticationRepository.getAuthority()).thenReturn(List.of(Authority.FREE)); MockHttpServletRequestBuilder requestBuilder = MockMvcRequestBuilders.multipart(testUrl) .file("excelFile", mockFile.getBytes()) @@ -140,7 +97,7 @@ void parsingFailTest() throws Exception { mvc.perform(requestBuilder).andExpect(MockMvcResultMatchers.status().isBadRequest()) .andExpect(result -> { byte[] response = result.getResponse().getContentAsByteArray(); - InvalidExcelDataResponse responseInfo = om.readValue(response, InvalidExcelDataResponse.class); + InvalidExcelDataResponse responseInfo = mapper.readValue(response, InvalidExcelDataResponse.class); assertThat(responseInfo.getTotalSize()).isEqualTo(failList.size()); assertThat(responseInfo.getFailRowIdx()).containsExactly(failIdx1, failIdx2); diff --git a/src/test/java/com/map/gaja/common/ControllerTest.java b/src/test/java/com/map/gaja/common/ControllerTest.java new file mode 100644 index 00000000..45df1365 --- /dev/null +++ b/src/test/java/com/map/gaja/common/ControllerTest.java @@ -0,0 +1,81 @@ +package com.map.gaja.common; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.map.gaja.client.application.*; +import com.map.gaja.client.application.validator.ClientRequestValidator; +import com.map.gaja.client.infrastructure.file.FileParsingService; +import com.map.gaja.client.infrastructure.file.FileValidator; +import com.map.gaja.client.infrastructure.geocode.Geocoder; +import com.map.gaja.client.presentation.api.ClientDeleteController; +import com.map.gaja.client.presentation.api.ClientSavingController; +import com.map.gaja.client.presentation.api.ClientUpdatingController; +import com.map.gaja.client.presentation.web.WebClientController; +import com.map.gaja.global.authentication.AuthenticationRepository; +import com.map.gaja.group.application.GroupAccessVerifyService; +import com.map.gaja.group.application.GroupService; +import com.map.gaja.group.presentation.api.GroupController; +import com.map.gaja.memo.application.MemoService; +import com.map.gaja.memo.presentation.api.MemoController; +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.data.jpa.mapping.JpaMetamodelMappingContext; +import org.springframework.test.web.servlet.MockMvc; + +@WebMvcTest( + controllers = { + GroupController.class, + ClientDeleteController.class, + ClientSavingController.class, + ClientUpdatingController.class, + WebClientController.class, + MemoController.class + } +) +@MockBean(JpaMetamodelMappingContext.class) +public abstract class ControllerTest { + @Autowired + protected MockMvc mvc; + + @Autowired + protected ObjectMapper mapper; + + @MockBean + protected ClientBulkService clientBulkService; + + @MockBean + protected ClientDeleteService clientDeleteService; + + @MockBean + protected ClientAccessVerifyService clientAccessVerifyService; + + @MockBean + protected AuthenticationRepository authenticationRepository; + + @MockBean + protected GroupAccessVerifyService groupAccessVerifyService; + + @MockBean + protected ClientRequestValidator clientRequestValidator; + + @MockBean + protected ClientSavingService clientSavingService; + + @MockBean + protected ClientUpdatingService clientUpdatingService; + + @MockBean + protected FileParsingService parsingService; + + @MockBean + protected GroupService groupService; + + @MockBean + protected Geocoder geocoder; + + @MockBean + protected FileValidator fileValidator; + + @MockBean + protected MemoService memoService; +} diff --git a/src/test/java/com/map/gaja/common/EventTest.java b/src/test/java/com/map/gaja/common/EventTest.java new file mode 100644 index 00000000..461ff716 --- /dev/null +++ b/src/test/java/com/map/gaja/common/EventTest.java @@ -0,0 +1,20 @@ +package com.map.gaja.common; + +import com.map.gaja.group.event.GroupEventListener; +import com.map.gaja.user.event.UserEventListener; +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.context.ApplicationEventPublisher; + +@SpringBootTest +public abstract class EventTest { + @Autowired + protected ApplicationEventPublisher publisher; + + @MockBean + protected GroupEventListener groupEventListener; + + @MockBean + protected UserEventListener userEventListener; +} diff --git a/src/test/java/com/map/gaja/common/NativeRepositoryTest.java b/src/test/java/com/map/gaja/common/NativeRepositoryTest.java new file mode 100644 index 00000000..96051511 --- /dev/null +++ b/src/test/java/com/map/gaja/common/NativeRepositoryTest.java @@ -0,0 +1,13 @@ +package com.map.gaja.common; + +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.context.annotation.Import; +import org.springframework.transaction.annotation.Transactional; + +@DataJpaTest +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +@Import(TestQuerydslConfig.class) +@Transactional +public abstract class NativeRepositoryTest { +} diff --git a/src/test/java/com/map/gaja/common/RepositoryTest.java b/src/test/java/com/map/gaja/common/RepositoryTest.java new file mode 100644 index 00000000..a86e4966 --- /dev/null +++ b/src/test/java/com/map/gaja/common/RepositoryTest.java @@ -0,0 +1,11 @@ +package com.map.gaja.common; + +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.context.annotation.Import; +import org.springframework.test.context.ActiveProfiles; + +@DataJpaTest +@ActiveProfiles("test") +@Import(TestQuerydslConfig.class) +public abstract class RepositoryTest { +} diff --git a/src/test/java/com/map/gaja/common/TestQuerydslConfig.java b/src/test/java/com/map/gaja/common/TestQuerydslConfig.java new file mode 100644 index 00000000..d617ed8a --- /dev/null +++ b/src/test/java/com/map/gaja/common/TestQuerydslConfig.java @@ -0,0 +1,54 @@ +package com.map.gaja.common; + +import com.map.gaja.client.infrastructure.repository.ClientBulkRepository; +import com.map.gaja.client.infrastructure.repository.ClientQueryRepository; +import com.map.gaja.client.infrastructure.repository.querydsl.sql.NativeSqlCreator; +import com.map.gaja.client.infrastructure.repository.querydsl.sql.PostgreSQLNativeSqlCreator; +import com.map.gaja.group.infrastructure.GroupQueryRepository; +import com.querydsl.jpa.impl.JPAQueryFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.jdbc.core.JdbcTemplate; + +import javax.persistence.EntityManager; +import javax.sql.DataSource; + +@TestConfiguration +public class TestQuerydslConfig { + @Autowired + private DataSource dataSource; + + @Autowired + private EntityManager entityManager; + + @Bean + public JPAQueryFactory jpaQueryFactory() { + return new JPAQueryFactory(entityManager); + } + + @Bean + public NativeSqlCreator nativeSqlCreator() { + return new PostgreSQLNativeSqlCreator(); + } + + @Bean + public JdbcTemplate jdbcTemplate() { + return new JdbcTemplate(dataSource); + } + + @Bean + public ClientQueryRepository clientQueryRepository() { + return new ClientQueryRepository(jpaQueryFactory(), nativeSqlCreator()); + } + + @Bean + public ClientBulkRepository clientBulkRepository() { + return new ClientBulkRepository(jdbcTemplate()); + } + + @Bean + public GroupQueryRepository groupQueryRepository() { + return new GroupQueryRepository(jpaQueryFactory()); + } +} \ No newline at end of file diff --git a/src/test/java/com/map/gaja/fixture/UserFixture.java b/src/test/java/com/map/gaja/fixture/UserFixture.java new file mode 100644 index 00000000..46cc084e --- /dev/null +++ b/src/test/java/com/map/gaja/fixture/UserFixture.java @@ -0,0 +1,47 @@ +package com.map.gaja.fixture; + +import com.map.gaja.user.domain.model.Authority; +import com.map.gaja.user.domain.model.User; + +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; + +public class UserFixture { + + /** + * Free등급 사용자 생성 + */ + public static User createFreeUser() { + return User.builder() + .id(1L) + .email("test@gmail.com") + .active(true) + .authority(Authority.FREE) + .groupCount(0) + .lastLoginDate(LocalDateTime.now()) + .build(); + } + + /** + * 회원 탈퇴한 사용자 생성 + */ + public static User createWithdrawnUser() { + return User.builder() + .email("test@gmail.com") + .active(false) + .build(); + } + + /** + * 최근 접속 날짜를 변경할 수 있는 사용자 생성 + */ + public static User createUserWithCustomLastLogin(LocalDateTime localDateTime) { + return User.builder() + .email("test@gmail.com") + .active(true) + .authority(Authority.FREE) + .groupCount(0) + .lastLoginDate(localDateTime) + .build(); + } +} diff --git a/src/test/java/com/map/gaja/group/event/GroupEventListenerTest.java b/src/test/java/com/map/gaja/group/event/GroupEventListenerTest.java index 52351240..df8f0851 100644 --- a/src/test/java/com/map/gaja/group/event/GroupEventListenerTest.java +++ b/src/test/java/com/map/gaja/group/event/GroupEventListenerTest.java @@ -1,5 +1,6 @@ package com.map.gaja.group.event; +import com.map.gaja.common.EventTest; import com.map.gaja.user.domain.model.User; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -11,14 +12,10 @@ import static org.mockito.Mockito.verify; -@SpringBootTest @Transactional -class GroupEventListenerTest { - @Autowired - ApplicationEventPublisher publisher; +class GroupEventListenerTest extends EventTest { + - @MockBean - GroupEventListener groupEventListener; @Test @DisplayName("그룹이 생성되면 이벤트 리스너가 실행된다.") diff --git a/src/test/java/com/map/gaja/group/infrastructure/GroupQueryRepositoryTest.java b/src/test/java/com/map/gaja/group/infrastructure/GroupQueryRepositoryTest.java index cabff425..c6dc6539 100644 --- a/src/test/java/com/map/gaja/group/infrastructure/GroupQueryRepositoryTest.java +++ b/src/test/java/com/map/gaja/group/infrastructure/GroupQueryRepositoryTest.java @@ -1,5 +1,6 @@ package com.map.gaja.group.infrastructure; +import com.map.gaja.common.RepositoryTest; import com.map.gaja.group.domain.model.Group; import com.map.gaja.user.domain.model.Authority; import com.map.gaja.user.domain.model.User; @@ -7,8 +8,6 @@ import org.junit.jupiter.api.DisplayName; 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; import javax.persistence.EntityManager; @@ -16,10 +15,8 @@ import static org.assertj.core.api.Assertions.*; -@SpringBootTest -@Transactional -class GroupQueryRepositoryTest { +class GroupQueryRepositoryTest extends RepositoryTest{ @Autowired GroupQueryRepository groupQueryRepository; diff --git a/src/test/java/com/map/gaja/group/infrastructure/GroupRepositoryTest.java b/src/test/java/com/map/gaja/group/infrastructure/GroupRepositoryTest.java index 02c868b8..95d79bcf 100644 --- a/src/test/java/com/map/gaja/group/infrastructure/GroupRepositoryTest.java +++ b/src/test/java/com/map/gaja/group/infrastructure/GroupRepositoryTest.java @@ -1,5 +1,6 @@ package com.map.gaja.group.infrastructure; +import com.map.gaja.common.RepositoryTest; import com.map.gaja.group.domain.model.Group; import com.map.gaja.group.presentation.dto.response.GroupInfo; import com.map.gaja.user.domain.model.Authority; @@ -18,9 +19,8 @@ import static org.junit.jupiter.api.Assertions.*; -@SpringBootTest -@Transactional -class GroupRepositoryTest { + +class GroupRepositoryTest extends RepositoryTest { @Autowired GroupRepository groupRepository; diff --git a/src/test/java/com/map/gaja/group/presentation/api/GroupControllerTest.java b/src/test/java/com/map/gaja/group/presentation/api/GroupControllerTest.java index f39927e3..63aad8c2 100644 --- a/src/test/java/com/map/gaja/group/presentation/api/GroupControllerTest.java +++ b/src/test/java/com/map/gaja/group/presentation/api/GroupControllerTest.java @@ -1,21 +1,14 @@ package com.map.gaja.group.presentation.api; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.map.gaja.global.authentication.AuthenticationRepository; +import com.map.gaja.common.ControllerTest; import com.map.gaja.global.authentication.PrincipalDetails; -import com.map.gaja.group.application.GroupService; import com.map.gaja.group.presentation.dto.request.GroupCreateRequest; import org.junit.jupiter.api.DisplayName; 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.data.jpa.mapping.JpaMetamodelMappingContext; import org.springframework.http.MediaType; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; -import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; @@ -25,23 +18,7 @@ import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -@WebMvcTest(controllers = GroupController.class) -@MockBean(JpaMetamodelMappingContext.class) -class GroupControllerTest { - @Autowired - MockMvc mvc; - - @MockBean - GroupService groupService; - - @MockBean - GroupController groupController; - - @MockBean - AuthenticationRepository authenticationRepository; - - @Autowired - ObjectMapper mapper; +class GroupControllerTest extends ControllerTest { @Test @DisplayName("@Valid 테스트") diff --git a/src/test/java/com/map/gaja/memo/infrastructure/MemoRepositoryTest.java b/src/test/java/com/map/gaja/memo/infrastructure/MemoRepositoryTest.java index 6d7780e4..be2d9c27 100644 --- a/src/test/java/com/map/gaja/memo/infrastructure/MemoRepositoryTest.java +++ b/src/test/java/com/map/gaja/memo/infrastructure/MemoRepositoryTest.java @@ -2,6 +2,7 @@ import com.map.gaja.TestEntityCreator; import com.map.gaja.client.domain.model.Client; +import com.map.gaja.common.RepositoryTest; import com.map.gaja.global.event.Events; import com.map.gaja.group.domain.model.Group; import com.map.gaja.memo.domain.model.Memo; @@ -12,12 +13,15 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.ApplicationEventPublisher; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Slice; import org.springframework.data.domain.Sort; +import org.springframework.test.context.ActiveProfiles; import org.springframework.transaction.annotation.Transactional; import javax.persistence.EntityManager; @@ -25,9 +29,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; -@SpringBootTest -@Transactional -class MemoRepositoryTest { + +class MemoRepositoryTest extends RepositoryTest { @Autowired MemoRepository memoRepository; diff --git a/src/test/java/com/map/gaja/memo/presentation/api/MemoControllerTest.java b/src/test/java/com/map/gaja/memo/presentation/api/MemoControllerTest.java index 1a5d2dc7..0b7b33ff 100644 --- a/src/test/java/com/map/gaja/memo/presentation/api/MemoControllerTest.java +++ b/src/test/java/com/map/gaja/memo/presentation/api/MemoControllerTest.java @@ -1,39 +1,23 @@ package com.map.gaja.memo.presentation.api; import com.fasterxml.jackson.databind.ObjectMapper; -import com.map.gaja.global.authentication.AuthenticationRepository; +import com.map.gaja.common.ControllerTest; import com.map.gaja.global.authentication.PrincipalDetails; -import com.map.gaja.memo.application.MemoService; import com.map.gaja.memo.presentation.dto.request.MemoCreateRequest; import org.junit.jupiter.api.DisplayName; 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.data.jpa.mapping.JpaMetamodelMappingContext; import org.springframework.http.MediaType; import org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors; -import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -@WebMvcTest(controllers = MemoController.class) -@MockBean(JpaMetamodelMappingContext.class) -class MemoControllerTest { - @Autowired - MockMvc mvc; - - @MockBean - MemoService memoService; - - @MockBean - AuthenticationRepository authenticationRepository; - +class MemoControllerTest extends ControllerTest { @Autowired ObjectMapper objectMapper; diff --git a/src/test/java/com/map/gaja/user/application/AutoLoginProcessorTest.java b/src/test/java/com/map/gaja/user/application/AutoLoginProcessorTest.java index 16e4345f..62086604 100644 --- a/src/test/java/com/map/gaja/user/application/AutoLoginProcessorTest.java +++ b/src/test/java/com/map/gaja/user/application/AutoLoginProcessorTest.java @@ -3,9 +3,9 @@ import com.map.gaja.client.infrastructure.repository.ClientQueryRepository; import com.map.gaja.client.infrastructure.s3.S3UrlGenerator; import com.map.gaja.client.presentation.dto.response.ClientOverviewResponse; +import com.map.gaja.fixture.UserFixture; import com.map.gaja.group.infrastructure.GroupRepository; import com.map.gaja.group.presentation.dto.response.GroupInfo; -import com.map.gaja.user.domain.model.Authority; import com.map.gaja.user.domain.model.User; import com.map.gaja.user.infrastructure.UserRepository; import org.junit.jupiter.api.DisplayName; @@ -15,7 +15,6 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; import java.util.Optional; @@ -45,12 +44,7 @@ class AutoLoginProcessorTest { @DisplayName("사용자가 최근에 참조한 전체 그룹 조회") void findWholeGroup() { // given - Long userId = 1L; - User user = User.builder() - .id(userId) - .lastLoginDate(LocalDateTime.now()) - .authority(Authority.FREE) - .build(); + User user = UserFixture.createFreeUser(); List clientList = new ArrayList<>(); given(userRepository.findById(anyLong())) @@ -60,18 +54,17 @@ void findWholeGroup() { .willReturn(clientList); // when - autoLoginProcessor.process(userId); + autoLoginProcessor.process(user.getId()); // then - verify(clientQueryRepository).findWholeGroupClients(userId, user.getAuthority().getClientLimitCount()); + verify(clientQueryRepository).findWholeGroupClients(anyLong(), anyInt()); } @Test @DisplayName("사용자가 최근에 참조한 특정 그룹 조회") void findGroup() { // given - Long userId = 1L; - User user = new User("test@gmail.com"); + User user = UserFixture.createFreeUser(); user.accessGroup(1L); List clients = new ArrayList<>(); GroupInfo groupInfo = new GroupInfo() { @@ -99,7 +92,7 @@ public Integer getClientCount() { .willReturn(clients); // when - autoLoginProcessor.process(userId); + autoLoginProcessor.process(user.getId()); // then verify(clientQueryRepository).findRecentGroupClients(user.getReferenceGroupId()); diff --git a/src/test/java/com/map/gaja/user/application/UserServiceHelperTest.java b/src/test/java/com/map/gaja/user/application/UserServiceHelperTest.java index 63689d99..59e33f8f 100644 --- a/src/test/java/com/map/gaja/user/application/UserServiceHelperTest.java +++ b/src/test/java/com/map/gaja/user/application/UserServiceHelperTest.java @@ -1,5 +1,6 @@ package com.map.gaja.user.application; +import com.map.gaja.fixture.UserFixture; import com.map.gaja.user.domain.model.User; import com.map.gaja.user.infrastructure.UserRepository; import org.junit.jupiter.api.DisplayName; @@ -22,26 +23,27 @@ class UserServiceHelperTest { @Test @DisplayName("회원 탈퇴한 유저가 로그인할 경우") void withdrawalUserLogin() { - String email = "test@gmail.com"; - User user = User.builder() - .email(email) - .active(false) - .build(); - - when(userRepository.findByEmail(email)).thenReturn(user); + // given + User user = UserFixture.createWithdrawnUser(); + when(userRepository.findByEmail(user.getEmail())) + .thenReturn(user); - assertThatThrownBy(()->userServiceHelper.loginByEmail(userRepository, email, "APP")); + // when, then + assertThatThrownBy(() -> userServiceHelper.loginByEmail(userRepository, user.getEmail(), "APP")); } @Test @DisplayName("신규 유저가 로그인할 경우") void newUserLogin() { + // given String email = "test@gmail.com"; - when(userRepository.findByEmail(email)).thenReturn(null); + + // when UserServiceHelper.loginByEmail(userRepository, email, "APP"); + // then verify(userRepository, times(1)).save(any()); } diff --git a/src/test/java/com/map/gaja/user/domain/model/UserTest.java b/src/test/java/com/map/gaja/user/domain/model/UserTest.java index 0b11730e..d5cdaef0 100644 --- a/src/test/java/com/map/gaja/user/domain/model/UserTest.java +++ b/src/test/java/com/map/gaja/user/domain/model/UserTest.java @@ -1,5 +1,6 @@ package com.map.gaja.user.domain.model; +import com.map.gaja.fixture.UserFixture; import com.map.gaja.global.event.Events; import com.map.gaja.user.event.AutoLoginSucceededEvent; import org.junit.jupiter.api.DisplayName; @@ -28,10 +29,7 @@ class UserTest { void updateLastLoginDateSuccess() { // given LocalDateTime beforeDate = LocalDateTime.now().minus(2, ChronoUnit.DAYS); - User user = User.builder() - .id(1L) - .lastLoginDate(beforeDate) - .build(); + User user = UserFixture.createUserWithCustomLastLogin(beforeDate); // when user.updateLastLoginDateIfDifferent(LocalDateTime.now()); @@ -45,9 +43,7 @@ void updateLastLoginDateSuccess() { void updateLastLoginDateFail() { // given LocalDateTime currentDate = LocalDateTime.now(); - User user = User.builder() - .lastLoginDate(currentDate) - .build(); + User user = UserFixture.createUserWithCustomLastLogin(currentDate); // when user.updateLastLoginDateIfDifferent(currentDate); diff --git a/src/test/java/com/map/gaja/user/event/UserEventListenerTest.java b/src/test/java/com/map/gaja/user/event/UserEventListenerTest.java index dc95126c..4447ab4a 100644 --- a/src/test/java/com/map/gaja/user/event/UserEventListenerTest.java +++ b/src/test/java/com/map/gaja/user/event/UserEventListenerTest.java @@ -1,24 +1,14 @@ package com.map.gaja.user.event; -import com.map.gaja.global.event.Events; +import com.map.gaja.common.EventTest; import org.junit.jupiter.api.DisplayName; 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.context.ApplicationEventPublisher; import java.time.LocalDateTime; import static org.mockito.Mockito.verify; -@SpringBootTest -class UserEventListenerTest { - @Autowired - ApplicationEventPublisher publisher; - - @MockBean - UserEventListener userEventListener; +class UserEventListenerTest extends EventTest { @Test @DisplayName("로그인에 성공하면 이벤트 리스너가 실행된다.") @@ -27,7 +17,7 @@ void loginSucceededEvent() { LoginSucceededEvent loginSucceededEvent = new LoginSucceededEvent("email", "APP"); // when - Events.raise(loginSucceededEvent); + publisher.publishEvent(loginSucceededEvent); // then verify(userEventListener).login(loginSucceededEvent); @@ -40,7 +30,7 @@ void withdrawnEvent() { WithdrawnEvent withdrawnEvent = new WithdrawnEvent("email"); // when - Events.raise(withdrawnEvent); + publisher.publishEvent(withdrawnEvent); // then verify(userEventListener).withdrawal(withdrawnEvent); @@ -53,7 +43,7 @@ void autoLoginSucceededEvent() { AutoLoginSucceededEvent autoLoginSucceededEvent = new AutoLoginSucceededEvent(1L, LocalDateTime.now()); // when - Events.raise(autoLoginSucceededEvent); + publisher.publishEvent(autoLoginSucceededEvent); // then verify(userEventListener).autoLogin(autoLoginSucceededEvent); diff --git a/src/test/java/com/map/gaja/user/infrastructure/UserRepositoryTest.java b/src/test/java/com/map/gaja/user/infrastructure/UserRepositoryTest.java index 8a47f315..d658c63b 100644 --- a/src/test/java/com/map/gaja/user/infrastructure/UserRepositoryTest.java +++ b/src/test/java/com/map/gaja/user/infrastructure/UserRepositoryTest.java @@ -1,12 +1,12 @@ package com.map.gaja.user.infrastructure; -import com.map.gaja.user.domain.model.Authority; +import com.map.gaja.common.RepositoryTest; +import com.map.gaja.fixture.UserFixture; import com.map.gaja.user.domain.model.User; + import org.junit.jupiter.api.DisplayName; 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; import javax.persistence.EntityManager; import java.time.LocalDateTime; @@ -14,9 +14,7 @@ import static org.assertj.core.api.Assertions.assertThat; -@SpringBootTest -@Transactional -class UserRepositoryTest { +class UserRepositoryTest extends RepositoryTest { @Autowired UserRepository userRepository; @@ -27,24 +25,20 @@ class UserRepositoryTest { @DisplayName("최근 접속 일을 변경한다.") void updateLastLoginDate() { // given - User user = User.builder() - .email("test") - .active(true) - .authority(Authority.FREE) - .groupCount(0) - .lastLoginDate(LocalDateTime.now().minus(2, ChronoUnit.DAYS)) - .build(); + LocalDateTime lastLoginTime = LocalDateTime.now().minus(2, ChronoUnit.DAYS); + User user = UserFixture.createUserWithCustomLastLogin(lastLoginTime); userRepository.save(user); - em.flush(); em.clear(); + Long userId = user.getId(); LocalDateTime now = LocalDateTime.now(); // when - userRepository.updateLastLoginDate(user.getId(), now); - user = userRepository.findByEmailAndActive("test").get(); + userRepository.updateLastLoginDate(userId, now); + User targetUser = userRepository.findById(userId).get(); // then - assertThat(user.getLastLoginDate().truncatedTo(ChronoUnit.SECONDS)).isEqualTo(now.truncatedTo(ChronoUnit.SECONDS)); + assertThat(targetUser.getLastLoginDate().truncatedTo(ChronoUnit.SECONDS)) + .isEqualTo(now.truncatedTo(ChronoUnit.SECONDS)); } } \ No newline at end of file diff --git a/src/test/resources/application-test.yml b/src/test/resources/application-test.yml new file mode 100644 index 00000000..38944b1e --- /dev/null +++ b/src/test/resources/application-test.yml @@ -0,0 +1,13 @@ +spring: + + datasource: + driver-class-name: org.h2.Driver + url: jdbc:h2:mem:test; + + jpa: + hibernate: + ddl-auto: create + properties: + hibernate: + format_sql: true + dialect: org.hibernate.spatial.dialect.h2geodb.GeoDBDialect