Issue
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 "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;
}
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<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"));
}
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?
Solution
Your type
is always "turbine"
, 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<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"));
}
Answered By - Mureinik