Java Spring2.0.0.0版本中的Mockito Bug
我用Spring的最新版本(2.0.0.RELEASE)更新了我的项目,虽然我的测试在2.0.0.RC1中工作,但现在它不工作了,它不断给我这个错误:Java Spring2.0.0.0版本中的Mockito Bug,java,spring,spring-boot,mockito,Java,Spring,Spring Boot,Mockito,我用Spring的最新版本(2.0.0.RELEASE)更新了我的项目,虽然我的测试在2.0.0.RC1中工作,但现在它不工作了,它不断给我这个错误: org.mockito.exceptions.base.MockitoException: Cannot instantiate @InjectMocks field named 'service'! Cause: the type 'PersonService' is an interface. You haven't provided th
org.mockito.exceptions.base.MockitoException:
Cannot instantiate @InjectMocks field named 'service'! Cause: the type 'PersonService' is an interface.
You haven't provided the instance at field declaration so I tried to construct the instance.
Examples of correct usage of @InjectMocks:
@InjectMocks Service service = new Service();
@InjectMocks Service service;
//and... don't forget about some @Mocks for injection :)
在这里,我做了一个最小的项目,您可以在pom文件中更改版本,以看到它在2.0.0.RC1上成功,在2.0.0.RELEASE中失败
对于完整的最小测试,请从
@InjectMocks
Mockito不能实例化内部类、本地类、抽象类,当然还有接口
在您的情况下,您应该在测试中使用:
@InjectMocks
private PersonServiceImpl underTest;
我已经从github查看了您的示例,如果您更改为服务的实现,测试将通过您希望在测试中使用Mockito和mocked的东西,还是希望Spring将PersonService bean注入到您的测试类中?到目前为止,这两个都有,这是没有意义的。我想在我的测试atm中使用Mockito和mocked的东西。然后你可以远程
Autowired
和SpringBootTest
注释。投票支持提供“最小测试复制”。通常的警告词,除了我对实际验证示例输入的支持外:此注释的最大缺点是它会无声地失败。如果它不能注入mock,它就什么也不做。这可能是一个真正的痛苦的数字。因此,我倾向于避免使用它。