在部署Sping Boot 应用程序时,我遇到了声纳扫描问题。为了解决这个问题,我需要编写85%的测试覆盖率。我为我的服务层编写了一些测试用例,但测试覆盖率仍然是72.1%。我想要一些可以添加到服务层测试中的测试用例。
JoinQueryService层:
@Service
public class 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)
public 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%。请根据以上服务层提供更多的测试案例。
1条答案
按热度按时间hgqdbh6s1#
这将是相当困难的,以确定这只是看代码。
然而,有一个很棒的IDE插件SonarLint,它将生成一个粒度级别的测试覆盖率报告。
步骤1.在您喜欢的IDE中安装SonarLint插件。
步骤2.运行应用程序“With Coverage”
步骤3.查看测试覆盖率报告