Spring Boot 需要测试用例来实现85%的测试覆盖率

xt0899hw  于 2023-11-17  发布在  Spring
关注(0)|答案(1)|浏览(176)

在部署Sping Boot 应用程序时,我遇到了声纳扫描问题。为了解决这个问题,我需要编写85%的测试覆盖率。我为我的服务层编写了一些测试用例,但测试覆盖率仍然是72.1%。我想要一些可以添加到服务层测试中的测试用例。
JoinQueryService层:

  1. @Service
  2. public class JoinQueryService {
  3. @Resource
  4. private EmployeeRepository employeeRepository;
  5. public List<DeptEmpDto> getCrediProduct(DeptEmpDto input) {
  6. List<DeptEmpDto> list=null;
  7. if (!input.getEmpName().isEmpty() && input.getEmpEmail().isEmpty()){
  8. list = employeeRepository.fetchEmpDeptDataInnerJoinByName(input.getEmpName());
  9. }else if(input.getEmpName().isEmpty() && !input.getEmpEmail().isEmpty()){
  10. list = employeeRepository.fetchEmpDeptDataInnerJoinByEmail(input.getEmpEmail());
  11. }else if(!input.getEmpName().isEmpty() && !input.getEmpEmail().isEmpty()){
  12. list = employeeRepository.fetchEmpDeptDataInnerJoin(input.getEmpName(), input.getEmpEmail());
  13. }else {
  14. throw new EmployeeDataNotFoundException();
  15. }
  16. return list;
  17. }
  18. public String deleteCreditProduct(DeptEmpDto input){
  19. if (!input.getEmpName().isEmpty() && input.getEmpEmail().isEmpty()){
  20. employeeRepository.deleteEmpDeptDataInnerJoinByName(input.getEmpName());
  21. }else if(input.getEmpName().isEmpty() && !input.getEmpEmail().isEmpty()){
  22. employeeRepository.deleteEmpDeptDataInnerJoinByEmail(input.getEmpEmail());
  23. }else if(!input.getEmpName().isEmpty() && !input.getEmpEmail().isEmpty()){
  24. employeeRepository.deleteEmpDeptDataInnerJoin(input.getEmpName(), input.getEmpEmail());
  25. }else {
  26. throw new EmployeeDataNotFoundException();
  27. }
  28. return "Deleted Successfully";
  29. }
  30. }

字符串
这已经为我的服务测试层编写了测试用例。

  1. @RunWith(MockitoJUnitRunner.class)
  2. public class JoinQueryServiceTest {
  3. @Mock
  4. private EmployeeRepository mockEmployeeRepository;
  5. @InjectMocks
  6. private JoinQueryService joinQueryServiceUnderTest;
  7. @Test
  8. public void testGetCrediProduct() {
  9. // Setup
  10. final DeptEmpDto input = new DeptEmpDto("empDept", "empName", "empEmail", "empAddress");
  11. // Configure EmployeeRepository.fetchEmpDeptDataInnerJoinByName(...).
  12. final List<DeptEmpDto> list = List.of(new DeptEmpDto("empDept", "empName", "empEmail", "empAddress"));
  13. when(mockEmployeeRepository.fetchEmpDeptDataInnerJoinByName("empName")).thenReturn(list);
  14. // Configure EmployeeRepository.fetchEmpDeptDataInnerJoinByEmail(...).
  15. final List<DeptEmpDto> list1 = List.of(new DeptEmpDto("empDept", "empName", "empEmail", "empAddress"));
  16. when(mockEmployeeRepository.fetchEmpDeptDataInnerJoinByEmail("empEmail")).thenReturn(list1);
  17. // Configure EmployeeRepository.fetchEmpDeptDataInnerJoin(...).
  18. final List<DeptEmpDto> list2 = List.of(new DeptEmpDto("empDept", "empName", "empEmail", "empAddress"));
  19. when(mockEmployeeRepository.fetchEmpDeptDataInnerJoin("empName", "empEmail")).thenReturn(list2);
  20. // Run the test
  21. final List<DeptEmpDto> result = joinQueryServiceUnderTest.getCrediProduct(input);
  22. // Verify the results
  23. }
  24. @Test
  25. public void testGetCrediProduct_EmployeeRepositoryFetchEmpDeptDataInnerJoinByNameReturnsNoItems() {
  26. // Setup
  27. final DeptEmpDto input = new DeptEmpDto("empDept", "empName", "empEmail", "empAddress");
  28. when(mockEmployeeRepository.fetchEmpDeptDataInnerJoinByName("empName")).thenReturn(Collections.emptyList());
  29. // Run the test
  30. final List<DeptEmpDto> result = joinQueryServiceUnderTest.getCrediProduct(input);
  31. // Verify the results
  32. assertEquals(Collections.emptyList(), result);
  33. }
  34. @Test
  35. public void testGetCrediProduct_EmployeeRepositoryFetchEmpDeptDataInnerJoinByEmailReturnsNoItems() {
  36. // Setup
  37. final DeptEmpDto input = new DeptEmpDto("empDept", "empName", "empEmail", "empAddress");
  38. when(mockEmployeeRepository.fetchEmpDeptDataInnerJoinByEmail("empEmail")).thenReturn(Collections.emptyList());
  39. // Run the test
  40. final List<DeptEmpDto> result = joinQueryServiceUnderTest.getCrediProduct(input);
  41. // Verify the results
  42. assertEquals(Collections.emptyList(), result);
  43. }
  44. @Test
  45. public void testGetCrediProduct_EmployeeRepositoryFetchEmpDeptDataInnerJoinReturnsNoItems() {
  46. // Setup
  47. final DeptEmpDto input = new DeptEmpDto("empDept", "empName", "empEmail", "empAddress");
  48. when(mockEmployeeRepository.fetchEmpDeptDataInnerJoin("empName", "empEmail")).thenReturn(
  49. Collections.emptyList());
  50. // Run the test
  51. final List<DeptEmpDto> result = joinQueryServiceUnderTest.getCrediProduct(input);
  52. // Verify the results
  53. assertEquals(Collections.emptyList(), result);
  54. }
  55. @Test
  56. public void testDeleteCreditProduct() {
  57. // Setup
  58. final DeptEmpDto input = new DeptEmpDto("empDept", "empName", "empEmail", "empAddress");
  59. // Run the test
  60. final String result = joinQueryServiceUnderTest.deleteCreditProduct(input);
  61. // Verify the results
  62. assertEquals("Deleted Successfully", result);
  63. verify(mockEmployeeRepository).deleteEmpDeptDataInnerJoinByName("empName");
  64. verify(mockEmployeeRepository).deleteEmpDeptDataInnerJoinByEmail("empEmail");
  65. verify(mockEmployeeRepository).deleteEmpDeptDataInnerJoin("empName", "empEmail");
  66. }
  67. }


我想要更多的测试案例来解决我的问题,覆盖率至少要从72.1%增加到85%。请根据以上服务层提供更多的测试案例。

hgqdbh6s

hgqdbh6s1#

这将是相当困难的,以确定这只是看代码。
然而,有一个很棒的IDE插件SonarLint,它将生成一个粒度级别的测试覆盖率报告。
步骤1.在您喜欢的IDE中安装SonarLint插件。
步骤2.运行应用程序“With Coverage”

步骤3.查看测试覆盖率报告

相关问题