You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:参考JUnit单条数据测试,编写0条和2条数据测试用例

JUnit测试用例实现:0条和2条数据场景

没问题,我来帮你完成这两个场景的JUnit测试用例!基于你已有的testfindOverviewByStatus()(单条数据场景),我们只需要调整Mock的返回数据,再针对性验证结果就可以了。假设你的测试类已经配置好了Mockito(比如用@Mock注入Service,@InjectMocks注入Controller),下面是具体实现:

1. 测试0条数据(空列表)场景

这个场景主要验证当Service返回空列表时,Controller的响应是否符合预期(比如返回200 OK,且空列表而非null):

@Test
public void testfindOverviewByStatus_EmptyList() {
    // 1. 准备空列表模拟数据
    List<DashboardOverview> emptyOverviewList = Collections.emptyList();
    
    // 2. Mock Service层的返回结果
    when(dashboardService.findOverviewByStatus(anyString())).thenReturn(emptyOverviewList);
    
    // 3. 调用Controller的目标方法
    ResponseEntity<List<DashboardOverview>> response = dashboardController.findOverviewByStatus("INACTIVE");
    
    // 4. 验证响应的正确性
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertNotNull(response.getBody()); // 确保返回的列表对象不为null
    assertEquals(0, response.getBody().size()); // 确认列表长度为0
}

关键说明:

  • 用Collections.emptyList()创建安全的空列表,避免后续操作出现空指针异常
  • 额外验证返回的Body不为null,这是很多接口的规范(返回空列表而非null)

2. 测试2条数据场景

这个场景需要模拟两条不同的业务数据,验证Controller返回的列表长度、每条数据的属性是否和Mock的一致:

@Test
public void testfindOverviewByStatus_TwoItems() {
    // 1. 创建两条模拟的DashboardOverview数据
    DashboardOverview overview1 = new DashboardOverview();
    overview1.setStatus("ACTIVE");
    overview1.setId(1L);
    overview1.setDashboardName("Sales Dashboard");
    
    DashboardOverview overview2 = new DashboardOverview();
    overview2.setStatus("ACTIVE");
    overview2.setId(2L);
    overview2.setDashboardName("Marketing Dashboard");
    
    // 2. 组装成包含两条数据的列表
    List<DashboardOverview> mockOverviewList = Arrays.asList(overview1, overview2);
    
    // 3. Mock Service层返回该列表(这里用eq()精确匹配参数,比anyString()更严谨)
    when(dashboardService.findOverviewByStatus(eq("ACTIVE"))).thenReturn(mockOverviewList);
    
    // 4. 调用Controller方法
    ResponseEntity<List<DashboardOverview>> response = dashboardController.findOverviewByStatus("ACTIVE");
    
    // 5. 验证结果
    assertEquals(HttpStatus.OK, response.getStatusCode());
    assertEquals(2, response.getBody().size()); // 确认列表长度为2
    
    // 验证每条数据的属性是否正确映射
    List<DashboardOverview> resultList = response.getBody();
    assertEquals("Sales Dashboard", resultList.get(0).getDashboardName());
    assertEquals(1L, resultList.get(0).getId().longValue());
    assertEquals("Marketing Dashboard", resultList.get(1).getDashboardName());
    assertEquals(2L, resultList.get(1).getId().longValue());
}

关键说明:

  • 用Arrays.asList()快速创建包含多元素的列表
  • 使用eq("ACTIVE")精确匹配入参,确保Controller正确传递了参数给Service
  • 不仅验证列表长度,还要校验每条数据的具体属性,保证业务逻辑的正确性

额外注意事项:

  • 确保你的测试类已经添加了Mockito的注解支持,比如:
    @ExtendWith(MockitoExtension.class) // JUnit 5
    // 或者 JUnit 4 的 @RunWith(MockitoJUnitRunner.class)
    public class DashboardControllerTest {
        @Mock
        private DashboardService dashboardService;
        
        @InjectMocks
        private DashboardController dashboardController;
    }
    

内容的提问来源于stack exchange,提问作者nico43624

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:06:02