gpt4 book ai didi

java - PowerMock - 无法将类 org.powermock.api.mockito.internal.mockmaker.PowerMockMaker 强制转换为接口(interface) org.mockito.plugins.MockMaker

转载 作者:行者123 更新时间:2023-11-30 08:00:15 29 4
gpt4 key购买 nike

使用 TestNg + PowerMock + Mockito 时遇到 java.lang.ExceptionInInitializerError:

Caused by: java.lang.NullPointerException
at org.mockito.internal.configuration.plugins.Plugins.getStackTraceCleanerProvider(Plugins.java:17)

问题的根源是:

java.lang.ClassCastException: Cannot cast class org.powermock.api.mockito.internal.mockmaker.PowerMockMaker to interface org.mockito.plugins.MockMaker

我的依赖树:

[INFO] +- org.powermock:powermock-api-mockito:jar:1.6.2:test
[INFO] | +- org.mockito:mockito-all:jar:1.10.19:test
[INFO] | \- org.powermock:powermock-api-support:jar:1.6.2:test
[INFO] | \- org.powermock:powermock-reflect:jar:1.6.2:test
[INFO] | \- org.objenesis:objenesis:jar:2.1:test
[INFO] +- org.powermock:powermock-module-testng:jar:1.6.2:test
[INFO] | +- org.powermock:powermock-core:jar:1.6.2:test
[INFO] | | \- org.javassist:javassist:jar:3.19.0-GA:test
[INFO] | \- org.powermock:powermock-module-testng-common:jar:1.6.2:test

测试类:

@PrepareForTest({CrossSiteServletRequestFilter.class})
@PowerMockIgnore({"org.apache.log4j.*"})
@Test
public class CrossSiteServletRequestFilterTest extends PowerMockTestCase{
@Mock
private HttpServletRequest request;

@BeforeSuite
public void beforeSuite() {
URL log4jCfg = CrossSiteServletRequestFilterTest.class.getClassLoader().getResource("log/log4j.xml");
DOMConfigurator.configure(log4jCfg);
MockitoAnnotations.initMocks(this); //<---It crashes here

我完全迷失了这个错误,感谢您的帮助。

最佳答案

您不应该将 MockitoAnnotations.initMocks 与 TestNG 一起使用。

Javadoc据说将其用于 JUnit(我认为 MockitoJUnitRunner/MockitoRule 是更好的方法),但不适用于 TestNG。

顺便说一句,当您使用 TestNG 时,PowerMockTestCase 相当于 MockitoJUnitRunner。因此,只需删除该行即可正常工作。

关于java - PowerMock - 无法将类 org.powermock.api.mockito.internal.mockmaker.PowerMockMaker 强制转换为接口(interface) org.mockito.plugins.MockMaker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32065666/

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