feat: 新增学习数据导出接口
This commit is contained in:
+65
@@ -0,0 +1,65 @@
|
||||
package com.guo.learningprogresstracker.controller;
|
||||
|
||||
import com.guo.learningprogresstracker.dto.ExportResult;
|
||||
import com.guo.learningprogresstracker.entity.CommonResult;
|
||||
import com.guo.learningprogresstracker.service.ExportService;
|
||||
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 java.time.LocalDate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* ExportController 纯单元测试(导出-01):直接 new 控制器并 mock 服务接口。
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class ExportControllerUnitTest {
|
||||
|
||||
@Mock
|
||||
private ExportService exportService;
|
||||
|
||||
@InjectMocks
|
||||
private ExportController controller;
|
||||
|
||||
@Test
|
||||
void exportTask_passesTaskNumAndFormat() throws Exception {
|
||||
ExportResult result = new ExportResult("a.md", "text/markdown; charset=UTF-8", "# 内容");
|
||||
when(exportService.exportTask("TASK-1", "markdown")).thenReturn(result);
|
||||
|
||||
CommonResult<ExportResult> response = controller.exportTask("TASK-1", "markdown");
|
||||
|
||||
assertEquals(200, response.getCode());
|
||||
assertSame(result, response.getData());
|
||||
}
|
||||
|
||||
@Test
|
||||
void exportRange_passesDatesAndFormat() throws Exception {
|
||||
ExportResult result = new ExportResult("a.json", "application/json; charset=UTF-8", "{}");
|
||||
when(exportService.exportRange(LocalDate.of(2026, 10, 1), LocalDate.of(2026, 10, 9), "json"))
|
||||
.thenReturn(result);
|
||||
|
||||
CommonResult<ExportResult> response = controller.exportRange(
|
||||
LocalDate.of(2026, 10, 1), LocalDate.of(2026, 10, 9), "json");
|
||||
|
||||
assertEquals(200, response.getCode());
|
||||
assertSame(result, response.getData());
|
||||
}
|
||||
|
||||
@Test
|
||||
void exportRange_nullDates_areForwardedAsNull() throws Exception {
|
||||
ExportResult result = new ExportResult("a.md", "text/markdown; charset=UTF-8", "# 内容");
|
||||
when(exportService.exportRange(null, null, "markdown")).thenReturn(result);
|
||||
|
||||
CommonResult<ExportResult> response = controller.exportRange(null, null, "markdown");
|
||||
|
||||
assertEquals(200, response.getCode());
|
||||
verify(exportService).exportRange(null, null, "markdown");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user