【发布时间】:2018-05-02 14:06:56
【问题描述】:
我正在尝试使用 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,但它在下载公司网络中的依赖项时带来了不同的挑战。
如果我需要添加更多代码或更多详细信息,请告诉我。
【问题讨论】:
-
你是如何运行测试的?
-
我用的是IntelliJ IDE,右键运行测试类。
-
嗯,您是否尝试过刷新您的 maven/gradle 项目的干净构建?
-
是的,我清除了目标文件夹,使用 mvn clean install -U 运行
-
ok.. 我建议您查看测试的运行配置。我可以说 eclipse:测试->右键单击->运行->运行配置。然后查看那里存在的依赖项。很可能您的实际依赖项可能与那些不匹配。
标签: java spring-boot mockito