From b3b546be83a9486f518d4c589ee86425539a1df5 Mon Sep 17 00:00:00 2001 From: Lee kangmin Date: Wed, 28 Aug 2024 18:33:02 +0900 Subject: [PATCH] =?UTF-8?q?[#54]=20coverage=20=EB=A5=BC=20=EC=9C=84?= =?UTF-8?q?=ED=95=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/StatisticsSchedulingTest.java | 76 +++++++++++ .../admin/service/StatisticsServiceTest.java | 77 +++++++++++ .../board/controller/BoardControllerTest.java | 35 +++-- .../controller/RecordControllerTest.java | 28 ++-- .../up/record/service/RecordServiceTest.java | 120 ++++++++++++++---- .../up/user/service/UserServiceTest.java | 2 +- 6 files changed, 284 insertions(+), 54 deletions(-) create mode 100644 src/test/java/com/speech/up/admin/service/StatisticsSchedulingTest.java create mode 100644 src/test/java/com/speech/up/admin/service/StatisticsServiceTest.java diff --git a/src/test/java/com/speech/up/admin/service/StatisticsSchedulingTest.java b/src/test/java/com/speech/up/admin/service/StatisticsSchedulingTest.java new file mode 100644 index 0000000..119f958 --- /dev/null +++ b/src/test/java/com/speech/up/admin/service/StatisticsSchedulingTest.java @@ -0,0 +1,76 @@ +package com.speech.up.admin.service; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import java.time.LocalDateTime; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import com.speech.up.admin.dto.StatisticsAdd; +import com.speech.up.admin.entity.StatisticsEntity; +import com.speech.up.admin.repository.StatisticsRepository; +import com.speech.up.record.repository.RecordRepository; +import com.speech.up.report.repository.ReportRepository; +import com.speech.up.script.repository.ScriptRepository; + +@SpringJUnitConfig +public class StatisticsSchedulingTest { + + @Mock + private ScriptRepository scriptRepository; + + @Mock + private ReportRepository reportRepository; + + @Mock + private RecordRepository recordRepository; + + @Mock + private StatisticsRepository statisticsRepository; + + @InjectMocks + private StatisticsScheduling statisticsScheduling; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @DisplayName("통계 주기적 업데이트 테스트") + @Test + void runStatisticsSetterTest() { + // Given + long scriptCount = 10L; + long reportCount = 20L; + long recordCount = 30L; + double avgScore = 5.5; + + StatisticsAdd.Request request = StatisticsAdd.Request + .builder() + .report(reportCount) + .record(recordCount) + .script(scriptCount) + .score(avgScore) + .createAt(LocalDateTime.now()) + .build(); + StatisticsEntity expected = StatisticsEntity.create(request); + when(statisticsRepository.save(any(StatisticsEntity.class))).thenReturn(expected); + + //when + statisticsScheduling.runStatisticsSetter(); + statisticsScheduling.statisticsSetter(); + + //then + assertEquals(expected.getReport(), reportCount); + assertEquals(expected.getRecord(), recordCount); + assertEquals(expected.getScript(), scriptCount); + assertEquals(expected.getScore(), avgScore); + } +} diff --git a/src/test/java/com/speech/up/admin/service/StatisticsServiceTest.java b/src/test/java/com/speech/up/admin/service/StatisticsServiceTest.java new file mode 100644 index 0000000..bb366bf --- /dev/null +++ b/src/test/java/com/speech/up/admin/service/StatisticsServiceTest.java @@ -0,0 +1,77 @@ +package com.speech.up.admin.service; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +import java.time.LocalDateTime; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import com.speech.up.admin.dto.StatisticsGet; +import com.speech.up.admin.entity.StatisticsEntity; +import com.speech.up.admin.repository.StatisticsRepository; + +public class StatisticsServiceTest { + + @Mock + StatisticsEntity statisticsEntity; + @Mock + StatisticsRepository statisticsRepository; + + @InjectMocks + StatisticsService statisticsService; + + Long id; + Long report; + Long script; + Long record; + double score; + LocalDateTime createAt; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + + id = 1L; + report = 1L; + script = 1L; + record = 1L; + createAt = LocalDateTime.now(); + score = 1.1; + + statisticsEntity = mock(StatisticsEntity.class); + + when(statisticsEntity.getId()).thenReturn(id); + when(statisticsEntity.getReport()).thenReturn(report); + when(statisticsEntity.getScript()).thenReturn(script); + when(statisticsEntity.getRecord()).thenReturn(record); + when(statisticsEntity.getScore()).thenReturn(score); + when(statisticsEntity.getCreateAt()).thenReturn(createAt); + + } + + @DisplayName("통계 가져오기") + @Test + void getStatisticsTest() { + //given + when(statisticsRepository.findTopByOrderByCreateAtDesc()).thenReturn(statisticsEntity); + + //when + StatisticsGet.Response expected = StatisticsGet.Response.toResponse(statisticsEntity); + StatisticsGet.Response actualResponse = statisticsService.getStatistics(); + + //then + assertEquals(expected.getCreateAt(), actualResponse.getCreateAt()); + assertEquals(expected.getReport(), actualResponse.getReport()); + assertEquals(expected.getScript(), actualResponse.getScript()); + assertEquals(expected.getRecord(), actualResponse.getRecord()); + assertEquals(expected.getScore(), actualResponse.getScore()); + + verify(statisticsRepository, times(1)).findTopByOrderByCreateAtDesc(); + } +} diff --git a/src/test/java/com/speech/up/board/controller/BoardControllerTest.java b/src/test/java/com/speech/up/board/controller/BoardControllerTest.java index b499f74..59143c2 100644 --- a/src/test/java/com/speech/up/board/controller/BoardControllerTest.java +++ b/src/test/java/com/speech/up/board/controller/BoardControllerTest.java @@ -3,16 +3,17 @@ import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; -import java.util.Collections; import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.http.ResponseEntity; +import com.speech.up.board.service.BoardService; import com.speech.up.board.service.dto.BoardAddDto; import com.speech.up.board.service.dto.BoardGetDto; import com.speech.up.board.service.dto.BoardIsUseDto; @@ -22,6 +23,9 @@ public class BoardControllerTest { @Mock + BoardService boardService; + + @InjectMocks private BoardController boardController; @BeforeEach @@ -33,30 +37,28 @@ public void setUp() { @Test public void getAllBoardsTest() { // given - List response = Collections.singletonList(mock(BoardGetDto.Response.class)); int page = 1; int size = 10; // when - when(boardController.getBoardAll(page, size)).thenReturn(ResponseEntity.ok(response)); + ResponseEntity> actualResponse = boardController.getBoardAll(page, size); //then - assertEquals(ResponseEntity.ok(response), boardController.getBoardAll(page, size)); + assertNotNull(actualResponse); } @DisplayName("getBoard 테스트") @Test public void getBoardTest() { //given - BoardGetDto.Response response = mock(BoardGetDto.Response.class); Long boardId = 1L; HttpServletRequest request = mock(HttpServletRequest.class); //when - when(boardController.getBoard(boardId, request)).thenReturn(ResponseEntity.ok(response)); + ResponseEntity actualResponse = boardController.getBoard(boardId, request); //then - assertEquals(ResponseEntity.ok(response), boardController.getBoard(boardId, request)); + assertNotNull(actualResponse); } @DisplayName("getBoardCount 테스트") @@ -67,10 +69,10 @@ public void getBoardCountTest() { Long boardId = 1L; //when - when(boardController.getBoardCount(request)).thenReturn(ResponseEntity.ok(boardId)); + ResponseEntity actualResponse = boardController.getBoardCount(request); //then - assertEquals(ResponseEntity.ok(boardId), boardController.getBoardCount(request)); + assertNotNull(actualResponse); } @DisplayName("addBoard 테스트") @@ -79,13 +81,12 @@ public void addBoardTest() { //given BoardAddDto.Request request = mock(BoardAddDto.Request.class); HttpServletRequest requestServlet = mock(HttpServletRequest.class); - BoardAddDto.Response response = mock(BoardAddDto.Response.class); //when - when(boardController.addBoard(request, requestServlet)).thenReturn(ResponseEntity.ok(response)); + ResponseEntity actualResponse = boardController.addBoard(request, requestServlet); //then - assertEquals(ResponseEntity.ok(response), boardController.addBoard(request, requestServlet)); + assertNotNull(actualResponse); } @DisplayName("updateBoard 테스트") @@ -93,13 +94,12 @@ public void addBoardTest() { public void updateBoardTest() { //given BoardUpdateDto.Request request = mock(BoardUpdateDto.Request.class); - BoardUpdateDto.Response response = mock(BoardUpdateDto.Response.class); //when - when(boardController.updateBoard(request)).thenReturn(ResponseEntity.ok(response)); + ResponseEntity actualResponse = boardController.updateBoard(request); //then - assertEquals(ResponseEntity.ok(response), boardController.updateBoard(request)); + assertNotNull(actualResponse); } @DisplayName("deleteBoard 테스트") @@ -107,12 +107,11 @@ public void updateBoardTest() { public void deleteBoardTest() { //given BoardIsUseDto.Request request = mock(BoardIsUseDto.Request.class); - BoardIsUseDto.Response response = mock(BoardIsUseDto.Response.class); //when - when(boardController.deleteBoard(request)).thenReturn(ResponseEntity.ok(response)); + ResponseEntity actualResponse = boardController.deleteBoard(request); //then - assertEquals(ResponseEntity.ok(response), boardController.deleteBoard(request)); + assertNotNull(actualResponse); } } \ No newline at end of file diff --git a/src/test/java/com/speech/up/record/controller/RecordControllerTest.java b/src/test/java/com/speech/up/record/controller/RecordControllerTest.java index 35b1b6c..d52eee4 100644 --- a/src/test/java/com/speech/up/record/controller/RecordControllerTest.java +++ b/src/test/java/com/speech/up/record/controller/RecordControllerTest.java @@ -4,7 +4,6 @@ import static org.mockito.Mockito.*; import java.io.IOException; -import java.util.Collections; import java.util.List; import javax.sound.sampled.UnsupportedAudioFileException; @@ -12,11 +11,13 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.http.ResponseEntity; import org.springframework.web.multipart.MultipartFile; +import com.speech.up.record.service.RecordService; import com.speech.up.record.service.dto.RecordAddDto; import com.speech.up.record.service.dto.RecordGetDto; import com.speech.up.record.service.dto.RecordIsUseDto; @@ -24,6 +25,9 @@ public class RecordControllerTest { @Mock + RecordService recordService; + + @InjectMocks RecordController recordController; @BeforeEach @@ -36,13 +40,12 @@ public void setUp() { public void getRecordAllTest() { //given Long scriptId = 1L; - List response = Collections.singletonList(mock(RecordGetDto.Response.class)); //when - when(recordController.getRecordALl(scriptId)).thenReturn(ResponseEntity.ok(response)); + ResponseEntity> actualResponse = recordController.getRecordALl(scriptId); //then - assertEquals(recordController.getRecordALl(scriptId), ResponseEntity.ok(response)); + assertNotNull(actualResponse); } @DisplayName("addRecord 테스트") @@ -56,9 +59,11 @@ public void addRecordTest() { //when try { - when(recordController.addRecord(file, languageCode, scriptId)).thenReturn(ResponseEntity.ok(response)); + ResponseEntity actualResponse = recordController.addRecord(file, languageCode, + scriptId); + //then - assertEquals(recordController.addRecord(file, languageCode, scriptId), ResponseEntity.ok(response)); + assertNotNull(actualResponse); } catch (UnsupportedAudioFileException | IOException e) { //then assertEquals("Test Exception", e.getMessage()); @@ -70,13 +75,12 @@ public void addRecordTest() { public void deleteRecordTest() { //given RecordIsUseDto.Request request = mock(RecordIsUseDto.Request.class); - ResponseEntity response = mock(String.valueOf(RecordIsUseDto.Response.class)); //when - when(recordController.deleteRecord(request)).thenReturn(response); + ResponseEntity actualResponse = recordController.deleteRecord(request); //then - assertEquals(recordController.deleteRecord(request), response); + assertNotNull(actualResponse); } @DisplayName("isRecordAnalyzed 테스트") @@ -84,11 +88,9 @@ public void deleteRecordTest() { public void isRecordAnalyzedTest() { //given Long recordId = 1L; - ResponseEntity response = mock(String.valueOf(Void.class)); //when - when(recordController.isRecordAnalyzed(recordId)).thenReturn(response); - + ResponseEntity actualResponse = recordController.isRecordAnalyzed(recordId); //then - assertEquals(recordController.isRecordAnalyzed(recordId), response); + assertNotNull(actualResponse); } } diff --git a/src/test/java/com/speech/up/record/service/RecordServiceTest.java b/src/test/java/com/speech/up/record/service/RecordServiceTest.java index 046a806..241d066 100644 --- a/src/test/java/com/speech/up/record/service/RecordServiceTest.java +++ b/src/test/java/com/speech/up/record/service/RecordServiceTest.java @@ -4,30 +4,94 @@ import static org.mockito.Mockito.*; import java.io.IOException; +import java.io.InputStream; +import java.time.LocalDateTime; import java.util.Collections; import java.util.List; +import java.util.Optional; import javax.sound.sampled.UnsupportedAudioFileException; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.web.multipart.MultipartFile; +import com.speech.up.record.entity.RecordEntity; +import com.speech.up.record.repository.RecordRepository; import com.speech.up.record.service.dto.RecordAddDto; import com.speech.up.record.service.dto.RecordGetDto; import com.speech.up.record.service.dto.RecordIsUseDto; +import com.speech.up.script.entity.ScriptEntity; +import com.speech.up.script.repository.ScriptRepository; +import com.speech.up.user.entity.UserEntity; public class RecordServiceTest { @Mock + RecordRepository recordRepository; + @Mock + ScriptRepository scriptRepository; + @Mock + ScriptEntity scriptEntity; + @Mock + UserEntity userEntity; + @Mock + RecordEntity recordEntity; + + @InjectMocks RecordService recordService; + Long scriptId; + Long userId; + String socialId; + String content; + String title; + boolean isUse; + LocalDateTime createdAt; + LocalDateTime updatedAt; + + Long recordId; + byte[] audio; + String languageCode; + boolean isAnalyzed; + @BeforeEach - public void setUp() { + void setUp() { MockitoAnnotations.openMocks(this); + + userId = 1L; + scriptId = 1L; + socialId = "socialId"; + content = "content"; + title = "title"; + isUse = true; + createdAt = LocalDateTime.now(); + updatedAt = LocalDateTime.now(); + recordId = 1L; + audio = new byte[] {1, 2, 3}; + languageCode = "en"; + isAnalyzed = false; + + userEntity = mock(UserEntity.class); + scriptEntity = mock(ScriptEntity.class); + recordEntity = mock(RecordEntity.class); + + when(userEntity.getUserId()).thenReturn(userId); + when(userEntity.getSocialId()).thenReturn(socialId); + when(scriptEntity.getScriptId()).thenReturn(scriptId); + when(scriptEntity.getTitle()).thenReturn(title); + when(scriptEntity.getContent()).thenReturn(content); + when(scriptEntity.getCreatedAt()).thenReturn(LocalDateTime.now()); + when(scriptEntity.getUser()).thenReturn(userEntity); + when(recordEntity.getRecordId()).thenReturn(recordId); + when(recordEntity.getCreatedAt()).thenReturn(LocalDateTime.now()); + when(recordEntity.getAudio()).thenReturn(audio); + when(recordEntity.getLanguageCode()).thenReturn(languageCode); + } @DisplayName("스크립트 아이디로 녹음 데이터 리스트 가져오기") @@ -35,59 +99,71 @@ public void setUp() { public void getRecordListByScriptIdTest() { //given List response = Collections.singletonList(mock(RecordGetDto.Response.class)); - Long scriptId = 1L; + List expected = Collections.singletonList(mock(RecordEntity.class)); + when(recordRepository.findByScriptScriptIdAndIsUseTrue(scriptId)).thenReturn(expected); //when - when(recordService.getRecordList(scriptId)).thenReturn(response); + List actualResponse = recordService.getRecordList(scriptId); //then - assertEquals(response, recordService.getRecordList(scriptId)); + assertEquals(response.size(), actualResponse.size()); } @DisplayName("녹음 데이터 추가하기") @Test - public void addRecordTest() { - //given + public void addRecordTest() throws IOException { + // Given MultipartFile file = mock(MultipartFile.class); + InputStream inputStream = mock(InputStream.class); + when(file.getInputStream()).thenReturn(inputStream); String languageCode = "korean"; Long scriptId = 1L; - RecordAddDto.Response response = mock(RecordAddDto.Response.class); + ScriptEntity scriptEntity = mock(ScriptEntity.class); + + when(scriptRepository.findById(scriptId)).thenReturn(Optional.of(scriptEntity)); + when(recordRepository.save(any(RecordEntity.class))).thenReturn(recordEntity); try { - //when - when(recordService.addRecord(file, languageCode, scriptId)).thenReturn(response); - //then - assertEquals(response, recordService.addRecord(file, languageCode, scriptId)); - } catch (UnsupportedAudioFileException | IOException e) { - //then - assertEquals("Test Exception", e.getMessage()); + // When + RecordAddDto.Response actualResponse = recordService.addRecord(file, languageCode, scriptId); + assertNotNull(actualResponse); + fail("Expected UnsupportedAudioFileException to be thrown"); + } catch (UnsupportedAudioFileException e) { + // Then + assertEquals("File of unsupported format", e.getMessage()); } } @DisplayName("녹음 데이터 삭제하기") @Test public void deleteRecordTest() { - //given + // given RecordIsUseDto.Request request = mock(RecordIsUseDto.Request.class); - RecordIsUseDto.Response response = mock(RecordIsUseDto.Response.class); + when(request.getRecordEntity()).thenReturn(recordEntity); + when(recordEntity.getRecordId()).thenReturn(recordId); - //when - when(recordService.deleteRecord(request)).thenReturn(response); + // when + RecordEntity recordResult = RecordEntity.delete(request); + when(recordRepository.save(any(RecordEntity.class))).thenReturn(recordEntity); + RecordIsUseDto.Response actualResponse = recordService.deleteRecord(request); - //then - assertEquals(response, recordService.deleteRecord(request)); + // then + assertEquals(recordResult.getRecordId(), actualResponse.getRecordId()); } @DisplayName("녹음 아이디로 분석 메서드 호출하기") @Test public void callAnalyzeMethodByRecordIdTest() { //given - Long recordId = 1L; + when(recordRepository.findById(recordId)).thenReturn(Optional.of(recordEntity)); + RecordEntity recordResult = RecordEntity.analyze(recordEntity); //when + when(recordRepository.save(any(RecordEntity.class))).thenReturn(recordEntity); recordService.analyzed(recordId); //then - verify(recordService, times(1)).analyzed(recordId); + assertEquals(recordResult.getRecordId(), recordId); + verify(recordRepository, times(1)).save(any(RecordEntity.class)); } } diff --git a/src/test/java/com/speech/up/user/service/UserServiceTest.java b/src/test/java/com/speech/up/user/service/UserServiceTest.java index 1418fa7..0ca5cc7 100644 --- a/src/test/java/com/speech/up/user/service/UserServiceTest.java +++ b/src/test/java/com/speech/up/user/service/UserServiceTest.java @@ -78,7 +78,7 @@ public void setUp() { servletRequest = mock(HttpServletRequest.class); - expectedEntity = new UserEntity(mockUserEntity); + expectedEntity = UserEntity.updateUserAccess(mockUserEntity); } @DisplayName("모든 유저 검색")