h-lab/src/test/java/com/hlab/yanalyst/domain/publish/PublishServiceTest.java
hehihoho3@gmail.com 2cdae03998 test: add unit test infrastructure and core-logic tests
First tests in the project (src/test was absent). Add testRuntimeOnly
junit-platform-launcher (required by Gradle 9 to load the JUnit Platform),
then cover:
- VideoMetrics: duration parse, isShorts boundary, viewsPerHour clamp/
  division, viewsPerSubRatio rounding & zero-guards (9 cases, pure)
- DashboardService.summary(): composes each domain service under the
  right key (Mockito)
- PublishService.dashboardSummary(): status counts + recent capped at 5

12 tests, all green. No Spring context / DB needed — fast.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-05-31 13:37:52 +09:00

68 lines
2.5 KiB
Java

package com.hlab.yanalyst.domain.publish;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.domain.Sort;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* 발행 대시보드 요약: 상태별 카운트 + 최근 5건(전체는 더 많아도 5개로 잘림).
*/
@ExtendWith(MockitoExtension.class)
class PublishServiceTest {
@Mock PublishPackageRepository repository;
@InjectMocks PublishService publishService;
@Test
void dashboardSummary_countsByStatusAndTakesFirstFiveAsRecent() {
when(repository.countByStatus("DRAFT")).thenReturn(2L);
when(repository.countByStatus("READY")).thenReturn(3L);
when(repository.countByStatus("PUBLISHED")).thenReturn(4L);
// list(null) -> repository.findAllSorted(sort); return 7 so recent should cap at 5
List<PublishPackage> seven = Collections.nCopies(7, mock(PublishPackage.class));
when(repository.findAllSorted(any(Sort.class))).thenReturn(seven);
Map<String, Object> summary = publishService.dashboardSummary();
@SuppressWarnings("unchecked")
Map<String, Long> byStatus = (Map<String, Long>) summary.get("byStatus");
assertThat(byStatus)
.containsEntry("DRAFT", 2L)
.containsEntry("READY", 3L)
.containsEntry("PUBLISHED", 4L);
assertThat(summary.get("total")).isEqualTo(7L);
@SuppressWarnings("unchecked")
List<PublishPackage> recent = (List<PublishPackage>) summary.get("recent");
assertThat(recent).hasSize(5);
}
@Test
void dashboardSummary_recentKeepsAllWhenFiveOrFewer() {
when(repository.countByStatus(any())).thenReturn(0L);
when(repository.findAllSorted(any(Sort.class)))
.thenReturn(Collections.nCopies(3, mock(PublishPackage.class)));
Map<String, Object> summary = publishService.dashboardSummary();
assertThat(summary.get("total")).isEqualTo(3L);
@SuppressWarnings("unchecked")
List<PublishPackage> recent = (List<PublishPackage>) summary.get("recent");
assertThat(recent).hasSize(3);
}
}