如何解决如何使用Mockito在Junit中测试开关盒
我可以测试代码,但是代码覆盖范围不包括第二种开关情况。
请参考以下代码。
{ @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"));
}
,
您不必编写两个不同的测试用例,而只需使用单个参数化的测试,即可为每次迭代设置不同的String值。
`@ParameterizedTest
@ValueSource(strings = {"value1","value2"})
void testMethod(String str) {
//testLogic
}`
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。