需要测试用例才能达到85%的测试覆盖率

问题描述 投票:0回答:1

在部署 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%。 请根据上述服务层为我提供更多测试用例。

spring-boot rest spring-data-jpa mockito junit4
1个回答
0
投票

仅查看代码就很难确定这一点。

但是,有一个很棒的 IDE 插件,称为 SonarLint,它将生成详细的测试覆盖率报告。

步骤 1. 在您最喜欢的 IDE 中安装 SonarLint 插件。

第 2 步:“覆盖”运行您的应用程序

第 3 步:查看测试覆盖率报告

© www.soinside.com 2019 - 2024. All rights reserved.