在部署 Spring Boot 应用程序时,我遇到声纳扫描问题。为了解决这个问题,我需要编写 85% 的测试覆盖率。我为我的服务层编写了一些测试用例,但测试覆盖率的结果仍然是72.1%。我想要一些可以添加到我的服务层测试中的测试用例。
加入查询服务层:
@服务 公共类 JoinQueryService {
@Resource
private EmployeeRepository employeeRepository;
public List<DeptEmpDto> getCrediProduct(DeptEmpDto input) {
List<DeptEmpDto> list=null;
if (!input.getEmpName().isEmpty() && input.getEmpEmail().isEmpty()){
list = employeeRepository.fetchEmpDeptDataInnerJoinByName(input.getEmpName());
}else if(input.getEmpName().isEmpty() && !input.getEmpEmail().isEmpty()){
list = employeeRepository.fetchEmpDeptDataInnerJoinByEmail(input.getEmpEmail());
}else if(!input.getEmpName().isEmpty() && !input.getEmpEmail().isEmpty()){
list = employeeRepository.fetchEmpDeptDataInnerJoin(input.getEmpName(), input.getEmpEmail());
}else {
throw new EmployeeDataNotFoundException();
}
return list;
}
public String deleteCreditProduct(DeptEmpDto input){
if (!input.getEmpName().isEmpty() && input.getEmpEmail().isEmpty()){
employeeRepository.deleteEmpDeptDataInnerJoinByName(input.getEmpName());
}else if(input.getEmpName().isEmpty() && !input.getEmpEmail().isEmpty()){
employeeRepository.deleteEmpDeptDataInnerJoinByEmail(input.getEmpEmail());
}else if(!input.getEmpName().isEmpty() && !input.getEmpEmail().isEmpty()){
employeeRepository.deleteEmpDeptDataInnerJoin(input.getEmpName(), input.getEmpEmail());
}else {
throw new EmployeeDataNotFoundException();
}
return "Deleted Successfully";
}
}
这已经为我的服务测试层编写了测试用例。
@RunWith(MockitoJUnitRunner.class) 公共类 JoinQueryServiceTest {
@Mock
private EmployeeRepository mockEmployeeRepository;
@InjectMocks
private JoinQueryService joinQueryServiceUnderTest;
@Test
public void testGetCrediProduct() {
// Setup
final DeptEmpDto input = new DeptEmpDto("empDept", "empName", "empEmail", "empAddress");
// Configure EmployeeRepository.fetchEmpDeptDataInnerJoinByName(...).
final List<DeptEmpDto> list = List.of(new DeptEmpDto("empDept", "empName", "empEmail", "empAddress"));
when(mockEmployeeRepository.fetchEmpDeptDataInnerJoinByName("empName")).thenReturn(list);
// Configure EmployeeRepository.fetchEmpDeptDataInnerJoinByEmail(...).
final List<DeptEmpDto> list1 = List.of(new DeptEmpDto("empDept", "empName", "empEmail", "empAddress"));
when(mockEmployeeRepository.fetchEmpDeptDataInnerJoinByEmail("empEmail")).thenReturn(list1);
// Configure EmployeeRepository.fetchEmpDeptDataInnerJoin(...).
final List<DeptEmpDto> list2 = List.of(new DeptEmpDto("empDept", "empName", "empEmail", "empAddress"));
when(mockEmployeeRepository.fetchEmpDeptDataInnerJoin("empName", "empEmail")).thenReturn(list2);
// Run the test
final List<DeptEmpDto> result = joinQueryServiceUnderTest.getCrediProduct(input);
// Verify the results
}
@Test
public void testGetCrediProduct_EmployeeRepositoryFetchEmpDeptDataInnerJoinByNameReturnsNoItems() {
// Setup
final DeptEmpDto input = new DeptEmpDto("empDept", "empName", "empEmail", "empAddress");
when(mockEmployeeRepository.fetchEmpDeptDataInnerJoinByName("empName")).thenReturn(Collections.emptyList());
// Run the test
final List<DeptEmpDto> result = joinQueryServiceUnderTest.getCrediProduct(input);
// Verify the results
assertEquals(Collections.emptyList(), result);
}
@Test
public void testGetCrediProduct_EmployeeRepositoryFetchEmpDeptDataInnerJoinByEmailReturnsNoItems() {
// Setup
final DeptEmpDto input = new DeptEmpDto("empDept", "empName", "empEmail", "empAddress");
when(mockEmployeeRepository.fetchEmpDeptDataInnerJoinByEmail("empEmail")).thenReturn(Collections.emptyList());
// Run the test
final List<DeptEmpDto> result = joinQueryServiceUnderTest.getCrediProduct(input);
// Verify the results
assertEquals(Collections.emptyList(), result);
}
@Test
public void testGetCrediProduct_EmployeeRepositoryFetchEmpDeptDataInnerJoinReturnsNoItems() {
// Setup
final DeptEmpDto input = new DeptEmpDto("empDept", "empName", "empEmail", "empAddress");
when(mockEmployeeRepository.fetchEmpDeptDataInnerJoin("empName", "empEmail")).thenReturn(
Collections.emptyList());
// Run the test
final List<DeptEmpDto> result = joinQueryServiceUnderTest.getCrediProduct(input);
// Verify the results
assertEquals(Collections.emptyList(), result);
}
@Test
public void testDeleteCreditProduct() {
// Setup
final DeptEmpDto input = new DeptEmpDto("empDept", "empName", "empEmail", "empAddress");
// Run the test
final String result = joinQueryServiceUnderTest.deleteCreditProduct(input);
// Verify the results
assertEquals("Deleted Successfully", result);
verify(mockEmployeeRepository).deleteEmpDeptDataInnerJoinByName("empName");
verify(mockEmployeeRepository).deleteEmpDeptDataInnerJoinByEmail("empEmail");
verify(mockEmployeeRepository).deleteEmpDeptDataInnerJoin("empName", "empEmail");
}
}
我想要更多的测试用例来解决我的问题,覆盖率至少应该从 72.1% 增加到 85%。 请根据上述服务层为我提供更多测试用例。