英文:
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 "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?
答案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 "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"));
}
答案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 = {"value1", "value2"})
void testMethod(String str) {
//testLogic
}`
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论