当使用TestNg + PowerMock + mockit时,我得到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。
BTW, PowerMockTestCase
在使用TestNG时相当于MockitoJUnitRunner
。所以,只要删除行,它应该可以工作。