用ORELSETHROW的可选写作Mockito单元测试:Jacoco给出0%代码覆盖率

发布于 2025-01-23 13:36:46 字数 2799 浏览 3 评论 0原文

我正在努力为以下逻辑编写单元测试,并具有可选的逻辑,而

class EmployeeHandler {
    public Employee getEmployeeById(Long id) {
        return employeeService.getEmployeeById(id)
            .map(apiMapper::toEmployeeOperationDTO)
            .orElseThrow(NoSuchElementException::new);
    }
}
 
public interface ApiMapper {
   EmployeeOperationDTO toEmployeeOperationDTO(EmployeeOperationn entity);
}

public class ApiMapperImpl implements ApiMapper {

    public EmployeeOperationDTO toEmployeeOperationDTO(EmployeeOperationn entity) {
        // EmployeeOperationDTO  Object creation logic
    }

}

class EmployeeOperationService {
    EmployeeOperationRepository employeeOperationRepo;
    public Optional<EmployeeOperation> getEmployeeById(Long id) {
        employeeOperationRepo.findById(id);
    }
}

我的测试

@Mock
private EmployeeOperationRepository employeeOperationRepo;
@Mock
private EmployeeOperationService employeeOperationService;
@MockApiMapper apiMapper;
@InjectMock
private EmployeeHandler employeeHandler;

@beforeEach
public void setUp() {
    MockitoAnnotations.initMocks(this);
}

@Test
public void getEmployeeById() {
    //getDto will create a sample dto object
    EmployeeOperationDTO dto = getDto();
    //getEoObject will create EmployeeOperation object
    EmployeeOperation eo = getEoObject();
    Long id = 1L;
    when(employeeOperationRepo.findById(id)).thenReturn(Optional.of(eo));
    doReturn(Optional.of(eo)).when(employeeOperationService).getEmployeeById(any());
    when(apiMapper.toEmployeeOperationDTO(eo )).thenReturn(dto);
    final EmployeeOperationDTO empDto = employeeHandler.getEmployeeById(id);
    Assertions.assertNotNull(empDto);
}

没有任何错误,但是Jacoco代码覆盖率为0%。另外,我也无法理解如何包括鼻孔的测试用例。

由于POM很大,因此我只是在这里添加摩知道依赖性。这是Mockito 4.0.0

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito.inline</artifactId>
<scope>test<test>
<dependency>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven.inline</artifactId>
<executions>
  <execution>
   <id>check</id>
    <goals>
     <goal>check</goal>
    </goals>
    <Configuration>
     <rules>
       <rule>
         <element>BUNDLE</element>
         <limits>
            <limit> 
               <counter>LINE</counter>
               <value>COVERAGERATIO</value>
               <minimum>0</minimum>
            </limit>  
         </limit>
       </rule>
     </rules>  
  </execution>
</executions>

I am struggling to write a unit test for below logic having Optional and orElseThrow

class EmployeeHandler {
    public Employee getEmployeeById(Long id) {
        return employeeService.getEmployeeById(id)
            .map(apiMapper::toEmployeeOperationDTO)
            .orElseThrow(NoSuchElementException::new);
    }
}
 
public interface ApiMapper {
   EmployeeOperationDTO toEmployeeOperationDTO(EmployeeOperationn entity);
}

public class ApiMapperImpl implements ApiMapper {

    public EmployeeOperationDTO toEmployeeOperationDTO(EmployeeOperationn entity) {
        // EmployeeOperationDTO  Object creation logic
    }

}

class EmployeeOperationService {
    EmployeeOperationRepository employeeOperationRepo;
    public Optional<EmployeeOperation> getEmployeeById(Long id) {
        employeeOperationRepo.findById(id);
    }
}

My Test

@Mock
private EmployeeOperationRepository employeeOperationRepo;
@Mock
private EmployeeOperationService employeeOperationService;
@MockApiMapper apiMapper;
@InjectMock
private EmployeeHandler employeeHandler;

@beforeEach
public void setUp() {
    MockitoAnnotations.initMocks(this);
}

@Test
public void getEmployeeById() {
    //getDto will create a sample dto object
    EmployeeOperationDTO dto = getDto();
    //getEoObject will create EmployeeOperation object
    EmployeeOperation eo = getEoObject();
    Long id = 1L;
    when(employeeOperationRepo.findById(id)).thenReturn(Optional.of(eo));
    doReturn(Optional.of(eo)).when(employeeOperationService).getEmployeeById(any());
    when(apiMapper.toEmployeeOperationDTO(eo )).thenReturn(dto);
    final EmployeeOperationDTO empDto = employeeHandler.getEmployeeById(id);
    Assertions.assertNotNull(empDto);
}

This case doesn't give any errors but Jacoco code coverage is 0%. Also, I am unable to understand how to include test case for NoSuchElementException as well.

Since the pom is huge, I am just adding the Mockito dependency here. It's Mockito 4.0.0

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito.inline</artifactId>
<scope>test<test>
<dependency>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven.inline</artifactId>
<executions>
  <execution>
   <id>check</id>
    <goals>
     <goal>check</goal>
    </goals>
    <Configuration>
     <rules>
       <rule>
         <element>BUNDLE</element>
         <limits>
            <limit> 
               <counter>LINE</counter>
               <value>COVERAGERATIO</value>
               <minimum>0</minimum>
            </limit>  
         </limit>
       </rule>
     </rules>  
  </execution>
</executions>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳公公是暖光 2025-01-30 13:36:46

您可以添加下面的语句

when(employeeOperationService.getEmployeeById(any())).thenReturn(Optional.empty());

,然后为您的测试案例投掷nosuchelementException

而且我想某些依赖关系在pom.xml中与jacoco兼容问题,这就是为什么您获得0%代码覆盖的原因。

https://github.com/mockito/mockito/mockito/mockito/mockito/issues/1717

You can add the statement below

when(employeeOperationService.getEmployeeById(any())).thenReturn(Optional.empty());

then it will throw a NoSuchElementException for your test case.

And I guess that some dependencies have compatible issue with Jacoco in your pom.xml, that's why you get 0% code coverage.

https://github.com/mockito/mockito/issues/1717

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文