如何使用 Mockito 在 Junit 中测试 switch case

How to test switch case in Junit using Mockito

我能够测试代码,但代码覆盖率不涵盖第二个开关案例。

请参考以下代码。

 { @PersistenceContext
    EntityManager manager;

    @Autowired
    TurbineRepository turbineRepository;

    @Autowired
    WorkRepository workRepository;

    public Dropdown getDropdown(String type) {
        Dropdown dropdownDTO = new Dropdown();
        switch(type) {
        case "turbine":
            List<String> turbinesList = turbineRepository.listOfTurbines();
            dropdownDTO.setTurbinesList(turbinesList);
            break;
        case "wocreate":
            List<String> turbineList = turbineRepository.listOfTurbines();
            dropdownDTO.setTurbinesList(turbineList);
            List<ParamsProjection> params = workRepository.findBy();
            Map<String, List<ParamsProjection>> result = params.stream()
                    .collect(Collectors.groupingBy(ParamsProjection::getType));
            dropdownDTO.setParams(result);
        default:
        }
        return dropdownDTO;

    }

下面是我的测试代码。

{
    @InjectMocks
    private Services service;
    
    @Mock
    private WorkRepository workRepo;
    
    @Mock
    private TurbineRepository turbineRepo;
    
    @Mock
    private ParamsProjection paramProject1;
    
    @Test 
    public void getDropDown() {
        
        Dropdown dto = new Dropdown();
        List<String> turbineList = new ArrayList<String>();
        String type = "turbine";
        switch(type) {
        case "turbine" :
        Mockito.when(turbineRepo.listOfTurbines()).thenReturn(turbineList);
        dto.setTurbinesList(turbineList);
        assertNotNull(dto);
        break;
        
        case "wocreate": 
        DropdownDTO dto2 = new DropdownDTO();
        Mockito.when(turbineRepo.listOfTurbines()).thenReturn(turbineList);
        dto2.setTurbinesList(turbineList);
        List<ParamsProjection> param = new ArrayList<ParamsProjection>();
        Mockito.when(workRepo.findBy()).thenReturn(param);
        
        Map<String, List<ParamsProjection>> result = param.stream()
                .collect(Collectors.groupingBy(ParamsProjection::getType));
        
        dto2.setParams(result);
        assertNotNull(dto2);
        break;
}
        assertNotNull(service.getDropdown("turbine"));
}

因为我已经声明了一个字符串变量,其值用于测试,所以我无法涵盖第二个 switch 语句。 我试过 if-else 情况,但出现同样的问题。 我们还有其他方法可以做到这一点吗?

您的 type 始终是 "turbine",因此只测试了这种情况。有两种不同的测试是有意义的,每种测试一种:

@Test 
public void getDropDownTurbine() {
    
    Dropdown dto = new Dropdown();
    List<String> turbineList = new ArrayList<String>();
    String type = "turbine";
    Mockito.when(turbineRepo.listOfTurbines()).thenReturn(turbineList);
    dto.setTurbinesList(turbineList);
    assertNotNull(dto);
    assertNotNull(service.getDropdown("turbine"));
} 


@Test 
public void getDropDown() {
    
    List<String> turbineList = new ArrayList<String>();
    String type = "wocreate";
    DropdownDTO dto2 = new DropdownDTO();
    Mockito.when(turbineRepo.listOfTurbines()).thenReturn(turbineList);
    dto2.setTurbinesList(turbineList);
    List<ParamsProjection> param = new ArrayList<ParamsProjection>();
    Mockito.when(workRepo.findBy()).thenReturn(param);
    
    Map<String, List<ParamsProjection>> result = param.stream()
            .collect(Collectors.groupingBy(ParamsProjection::getType));
    
    dto2.setParams(result);
    assertNotNull(dto2);

    assertNotNull(service.getDropdown("wocreate"));
} 

您可以简单地使用单个参数化测试,而不是编写两个不同的测试用例,您可以在其中为每次迭代设置不同的字符串值。

`@ParameterizedTest
 @ValueSource(strings = {"value1", "value2"})
 void testMethod(String str) {
        //testLogic
 }`