-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[#54] coverage 를 위한 테스트 코드 수정
- Loading branch information
Showing
6 changed files
with
284 additions
and
54 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
src/test/java/com/speech/up/admin/service/StatisticsSchedulingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/test/java/com/speech/up/admin/service/StatisticsServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.