用ORELSETHROW的可选写作Mockito单元测试:Jacoco给出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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以添加下面的语句
,然后为您的测试案例投掷
nosuchelementException
。而且我想某些依赖关系在
pom.xml
中与jacoco兼容问题,这就是为什么您获得0%代码覆盖的原因。https://github.com/mockito/mockito/mockito/mockito/mockito/issues/1717
You can add the statement below
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