我正在尝试使用Mockito 2.18.3框架模拟我们公司内部库中可用的final class
,遗憾的是我们无权更改库中的代码。但每当我跑步时,我都会得到以下错误:
java.lang.NoClassDefFoundError: Could not initialize class org.mockito.Mockito
at org.springframework.boot.test.mock.mockito.MockReset.get(MockReset.java:107)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:69)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.resetMocks(ResetMocksTestExecutionListener.java:55)
at org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener.afterTestMethod(ResetMocksTestExecutionListener.java:50)
at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:319)
这是我的依赖:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.18.3</version>
<scope>test</scope>
</dependency>
这是测试类:
@RunWith(SpringRunner.class)
@TestPropertySource("classpath:application-test.properties")
@SpringBootTest
public class JwtTokenTest {
@Autowired
private class JwtValidatorService jwtValidatorService;
@Mock
private JwtTokenDetails jwtTokenDetails;
@Test
public void jwtGenerateTest() {
//Code to test JWT generation
}
}
同样根据此链接:https://github.com/mockito/mockito/wiki/What%27s-new-in-Mockito-2#unmockable我创建了org.mockito.plugins.MockMaker
文件,内容为:mock-maker-inline
。
我尝试在其他Stackoverflow帖子和Google中搜索,但仍然没有解决方案。有谁可以帮助我吗?看起来我错过了什么,但未能识别出来。由于我在Mockito中没有太多的专业知识,所以尝试使用powermock,但是在下载公司网络中的依赖关系方面存在不同的挑战。
如果我需要添加更多代码或更多详细信息,请与我们联系。
答案 0 :(得分:1)
Spring Boot默认情况下使用Mockito 1.x 。但是,可以用Mockito 2.x覆盖它,因为Spring Boot也可以毫无问题地使用它(请参阅this commit)。
要这样做,只需将此修改添加到您的POM文件属性中即可:
<properties>
<mockito.version>2.18.3</mockito.version>
</properties>
答案 1 :(得分:0)
Spring Boot 1.5.11与Mockito 1.x兼容。具体来说,它使用1.10.19。您应该让Spring Boot的依赖管理指定版本,而不是将Mockito的版本覆盖到新的主要版本。这样做可确保您使用兼容版本。如果Mockito 1.10无法满足您的需求,您需要找到替代解决方案。