使用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类转换为org.mockito.plugins.MockMaker接口(interface),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32065666/