当前位置: 首页 > 知识库问答 >
问题:

如何使用Mockito在Junit中测试开关用例

邢飞白
2023-03-14

我能够测试代码,但代码覆盖率不包括第二个开关情况。

请参考以下代码。

 { @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的情况,但同样的问题发生了。我们还有别的办法吗?

共有2个答案

薛彭薄
2023-03-14

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

`@ParameterizedTest
 @ValueSource(strings = {"value1", "value2"})
 void testMethod(String str) {
        //testLogic
 }`
华甫
2023-03-14

您的类型始终是“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"));
} 
 类似资料:
  • 我对junit mockito非常陌生,并尝试使用mockito编写junit测试用例。 这是我的方法,我必须为此编写一个jUnit。 ChefService和ChefApi传递的方法参数来自第三方api 这里是呼叫chefService。listCookbookVersions()将返回CookBookVersion类类型的迭代器,如

  • 我有一个使用JSONObject的函数,我需要测试它。下面是我的代码: 这是我想测试的代码: 谢谢

  • 我有这个过滤器类,在使用junit进行测试时需要尽可能高的代码覆盖率。 和测试等级: 当我运行时,它在 线 我如何避免这种情况? 我需要调用这个方法并执行里面的任何内容来提供所需的代码覆盖。

  • 我在我的Java,Spring Boot控制器中创建了一个函数,它允许我根据参数获得数据的和值,这很有效。然而,我很难理解用Junit和Mockito测试这个功能的最佳方式是什么?到目前为止,我已经创建了一个测试函数,它返回一个特定数组字段的值。如何能够返回。thenreturn()中的值,该值根据给定的serviceID求和?任何帮助或建议任何其他有用的帖子将被感谢,因为我无法找到任何相关的或我

  • 我想使用Mockito对toEntity函数执行一个junit测试。 java.lang.NullPointerException org.mockito.exceptions.misusing.InvalidUseOfMatchersException:在此处检测到错误放置的参数匹配器: ->在com.example.mytest.setup(mytest.java:38) 不能在验证或短截之外

  • 我有一个类需要进行单元测试: 我有一个测试用例: 当我运行测试用例时,没有使用ActiveMQConnectionFactory的模拟对象。相反,正在使用实际的实现,并且正在建立TCP连接: 我尝试了Powermockito和Mockito,但都失败了。如何模拟对象,如何成功运行测试用例? 我是单元测试新手,试图从各个社区获得帮助,但没有找到合适的答案。任何帮助都将不胜感激。谢谢