Skip to content

Commit

Permalink
Merge pull request #185 from Kernel360/feature/#54/test
Browse files Browse the repository at this point in the history
[#54] coverage 를 위한 테스트 코드 수정
  • Loading branch information
km2535 authored Aug 28, 2024
2 parents a7d5142 + b3b546b commit 22f1d09
Show file tree
Hide file tree
Showing 6 changed files with 284 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +23,9 @@

public class BoardControllerTest {
@Mock
BoardService boardService;

@InjectMocks
private BoardController boardController;

@BeforeEach
Expand All @@ -33,30 +37,28 @@ public void setUp() {
@Test
public void getAllBoardsTest() {
// given
List<BoardGetDto.Response> response = Collections.singletonList(mock(BoardGetDto.Response.class));
int page = 1;
int size = 10;

// when
when(boardController.getBoardAll(page, size)).thenReturn(ResponseEntity.ok(response));
ResponseEntity<List<BoardGetDto.Response>> 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<BoardGetDto.Response> actualResponse = boardController.getBoard(boardId, request);

//then
assertEquals(ResponseEntity.ok(response), boardController.getBoard(boardId, request));
assertNotNull(actualResponse);
}

@DisplayName("getBoardCount 테스트")
Expand All @@ -67,10 +69,10 @@ public void getBoardCountTest() {
Long boardId = 1L;

//when
when(boardController.getBoardCount(request)).thenReturn(ResponseEntity.ok(boardId));
ResponseEntity<Long> actualResponse = boardController.getBoardCount(request);

//then
assertEquals(ResponseEntity.ok(boardId), boardController.getBoardCount(request));
assertNotNull(actualResponse);
}

@DisplayName("addBoard 테스트")
Expand All @@ -79,40 +81,37 @@ 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<BoardAddDto.Response> actualResponse = boardController.addBoard(request, requestServlet);

//then
assertEquals(ResponseEntity.ok(response), boardController.addBoard(request, requestServlet));
assertNotNull(actualResponse);
}

@DisplayName("updateBoard 테스트")
@Test
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<BoardUpdateDto.Response> actualResponse = boardController.updateBoard(request);

//then
assertEquals(ResponseEntity.ok(response), boardController.updateBoard(request));
assertNotNull(actualResponse);
}

@DisplayName("deleteBoard 테스트")
@Test
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<BoardIsUseDto.Response> actualResponse = boardController.deleteBoard(request);

//then
assertEquals(ResponseEntity.ok(response), boardController.deleteBoard(request));
assertNotNull(actualResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,30 @@
import static org.mockito.Mockito.*;

import java.io.IOException;
import java.util.Collections;
import java.util.List;

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.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;

public class RecordControllerTest {

@Mock
RecordService recordService;

@InjectMocks
RecordController recordController;

@BeforeEach
Expand All @@ -36,13 +40,12 @@ public void setUp() {
public void getRecordAllTest() {
//given
Long scriptId = 1L;
List<RecordGetDto.Response> response = Collections.singletonList(mock(RecordGetDto.Response.class));

//when
when(recordController.getRecordALl(scriptId)).thenReturn(ResponseEntity.ok(response));
ResponseEntity<List<RecordGetDto.Response>> actualResponse = recordController.getRecordALl(scriptId);

//then
assertEquals(recordController.getRecordALl(scriptId), ResponseEntity.ok(response));
assertNotNull(actualResponse);
}

@DisplayName("addRecord 테스트")
Expand All @@ -56,9 +59,11 @@ public void addRecordTest() {

//when
try {
when(recordController.addRecord(file, languageCode, scriptId)).thenReturn(ResponseEntity.ok(response));
ResponseEntity<RecordAddDto.Response> 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());
Expand All @@ -70,25 +75,22 @@ public void addRecordTest() {
public void deleteRecordTest() {
//given
RecordIsUseDto.Request request = mock(RecordIsUseDto.Request.class);
ResponseEntity<RecordIsUseDto.Response> response = mock(String.valueOf(RecordIsUseDto.Response.class));

//when
when(recordController.deleteRecord(request)).thenReturn(response);
ResponseEntity<RecordIsUseDto.Response> actualResponse = recordController.deleteRecord(request);

//then
assertEquals(recordController.deleteRecord(request), response);
assertNotNull(actualResponse);
}

@DisplayName("isRecordAnalyzed 테스트")
@Test
public void isRecordAnalyzedTest() {
//given
Long recordId = 1L;
ResponseEntity<Void> response = mock(String.valueOf(Void.class));
//when
when(recordController.isRecordAnalyzed(recordId)).thenReturn(response);

ResponseEntity<Void> actualResponse = recordController.isRecordAnalyzed(recordId);
//then
assertEquals(recordController.isRecordAnalyzed(recordId), response);
assertNotNull(actualResponse);
}
}
Loading

0 comments on commit 22f1d09

Please sign in to comment.