java - 使用 orElseThrow : Jacoco gives 0% code coverage 为Optional编写Mockito单元测试

标签 java unit-testing junit mockito jacoco

我正在努力为具有Optional和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);
    }
}

我的测试

@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%。另外,我也无法理解如何包含 NoSuchElementException 的测试用例。

由于 pom 很大,我只是在这里添加 Mockito 依赖项。这是 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>

最佳答案

您可以添加下面的语句

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

然后它将为您的测试用例抛出 NoSuchElementException

我猜想您的 pom.xml 中的某些依赖项与 Jacoco 存在兼容问题,这就是为什么您的代码覆盖率为 0%。

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

关于java - 使用 orElseThrow : Jacoco gives 0% code coverage 为Optional编写Mockito单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71990704/

相关文章:

python - 从 Python 测试用例中更改 "settings"值的最佳方法?

java - 为许多不同的配置文件集测试一个 Spring 测试类

Android Espresso 测试 'Cannot resolve symbol ' InstrumentationRegistry''

java - 在 RCP 应用程序中使用反射实现注释扫描的问题

变量的java枚举访问问题

java - 限制子类字段类型

java - 如何正确地对可打包对象进行单元测试?

Java 泛型转换错误

unit-testing - 单元测试文档

c# - 转换一个实现接口(interface)中所有方法的类