gpt4 book ai didi

java - Mockito 2 - SpringBoot 1.5.11.RELEASE - 无法初始化类 org.mockito.Mockito

转载 作者:行者123 更新时间:2023-11-30 06:11:33 37 4
gpt4 key购买 nike

我正在尝试使用 Mockito 2.18.3 框架模拟我们公司内部库中提供的 final 类,不幸的是我们无权更改库中的代码。但每当我运行时,我都会收到以下错误:

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,但它在下载公司网络中的依赖项时提出了不同的挑战。

如果我需要添加更多代码或更多详细信息,请告诉我。

最佳答案

Spring Boot 1.5.11 与 Mockito 1.x 兼容。具体来说,它使用 1.10.19。您应该让 Spring Boot 的依赖管理指定版本,而不是将 Mockito 的版本覆盖为新的主要版本。这样做将确保您使用兼容的版本。如果 Mockito 1.10 不能满足您的需求,您需要寻找替代解决方案。

关于java - Mockito 2 - SpringBoot 1.5.11.RELEASE - 无法初始化类 org.mockito.Mockito,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50136652/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com