【问题标题】:How to test switch case in Junit using Mockito如何使用 Mockito 在 Junit 中测试 switch case
【发布时间】:2020-09-04 21:22:52
【问题描述】:

我能够测试代码,但代码覆盖率不包括第二个 switch 案例。

请参考以下代码。

 { @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"));
}

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

【问题讨论】:

    标签: java junit mockito switch-statement


    【解决方案1】:

    您的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"));
    } 
    

    【讨论】:

    • 谢谢。它适用于两个单独的测试用例......有没有办法在一个测试用例中做到这一点?
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2022-11-03
      • 1970-01-01
      • 1970-01-01
      • 2021-11-06
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多