I am getting an java.lang.ExceptionInInitializerError when using TestNg + PowerMock + Mockito:
Caused by: java.lang.NullPointerException
at org.mockito.internal.configuration.plugins.Plugins.getStackTraceCleanerProvider(Plugins.java:17)
The root of the problem is:
java.lang.ClassCastException: Cannot cast class org.powermock.api.mockito.internal.mockmaker.PowerMockMaker to interface org.mockito.plugins.MockMaker
My dependency tree:
[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
Test class:
@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
I am completely lost with this error and appreciate any help.
You are not supposed to use MockitoAnnotations.initMocks
with TestNG.
The Javadoc is saying to use it for JUnit (and I think MockitoJUnitRunner
/ MockitoRule
is a better way) but not for TestNG.
BTW, PowerMockTestCase
is the equivalent of MockitoJUnitRunner
when you use TestNG. So, just remove the line and it should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.