如何在JUnit中使用Mockito测试switch case。

huangapple go评论65阅读模式
英文:

How to test switch case in Junit using Mockito

问题

我可以测试代码,但代码覆盖率未覆盖第二个 switch case。

请参考下面的代码。

{ 
    @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 语句,但出现了相同的问题。
我们还有其他方法可以解决这个问题吗?

英文:

I am able to test the code but code coverage does not cover second switch case.

Please refer the below code.

 { @PersistenceContext
	EntityManager manager;

	@Autowired
	TurbineRepository turbineRepository;

	@Autowired
	WorkRepository workRepository;

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

	}

Below is my test code.

{
    @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&lt;String&gt; turbineList = new ArrayList&lt;String&gt;();
		String type = &quot;turbine&quot;;
		switch(type) {
		case &quot;turbine&quot; :
		Mockito.when(turbineRepo.listOfTurbines()).thenReturn(turbineList);
		dto.setTurbinesList(turbineList);
		assertNotNull(dto);
		break;
		
		case &quot;wocreate&quot;: 
		DropdownDTO dto2 = new DropdownDTO();
		Mockito.when(turbineRepo.listOfTurbines()).thenReturn(turbineList);
		dto2.setTurbinesList(turbineList);
		List&lt;ParamsProjection&gt; param = new ArrayList&lt;ParamsProjection&gt;();
		Mockito.when(workRepo.findBy()).thenReturn(param);
		
		Map&lt;String, List&lt;ParamsProjection&gt;&gt; result = param.stream()
				.collect(Collectors.groupingBy(ParamsProjection::getType));
		
		dto2.setParams(result);
		assertNotNull(dto2);
		break;
}
        assertNotNull(service.getDropdown(&quot;turbine&quot;));
}

As I have declared a string variable with value for testing , I am not able to cover the second switch statement.
I have tried if-else case but same problem occurs.
Is there any other way we can do this?

答案1

得分: 1

@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"));
} 
英文:

Your type is always &quot;turbine&quot;, so just that case is tested. It would make sense to have two different tests, one for each type:

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


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

    assertNotNull(service.getDropdown(&quot;wocreate&quot;));
} 

答案2

得分: 1

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

@ParameterizedTest
@ValueSource(strings = {"value1", "value2"})
void testMethod(String str) {
    // 测试逻辑
}
英文:

Instead of writing two different test cases you can simply use single parameterized test in which you can set different String value for each iteration.

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

huangapple
  • 本文由 发表于 2020年8月31日 13:22:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63665193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定