Hi,
I tried to start using PM in a project. As soon as I annotate the test class with @PrepareForTest, even if I'm not adding any PM spy yet, I got strange mockito exception in one test out of the 11 in the class.
java.lang.ExceptionInInitializerError
at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.<init>(ConditionalStackTraceFilter.java:17)
at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:30)
at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:19)
at org.mockito.exceptions.misusing.MockitoConfigurationException.<init>(MockitoConfigurationException.java:18)
at org.mockito.internal.configuration.ClassPathLoader.loadImplementations(ClassPathLoader.java:145)
at org.mockito.internal.configuration.ClassPathLoader.findPluginImplementation(ClassPathLoader.java:110)
at org.mockito.internal.configuration.ClassPathLoader.findPlatformMockMaker(ClassPathLoader.java:106)
at org.mockito.internal.configuration.ClassPathLoader.<clinit>(ClassPathLoader.java:59)
at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:21)
at org.mockito.internal.MockitoCore.<init>(MockitoCore.java:40)
at org.mockito.Mockito.<clinit>(Mockito.java:932)
at MyTest.setupServletContext(AbstractPortkeyTest.java:83)
at MyTest.setupMocks(TestImageUploadControllers.java:52)
Caused by: java.lang.NullPointerException
at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.<clinit>(StackTraceFilter.java:21)
... 49 more
Note there is an exception thrown from inside of the MockitoConfigurationException constructor! The real exception thrown is in ClassPathLoader
throw new MockitoConfigurationException(
"Failed to load " + service + " using " + resource, e);
My test classes roughly look like
@Test(groups = {"auto"})
@PowerMockIgnore({"javax.management.*", "javax.xml.parsers.*",
"com.sun.org.apache.xerces.internal.jaxp.*", "com.sun.org.apache.xerces.internal.impl.dv.*",
"ch.qos.logback.*", "org.slf4j.*" // see http://stackoverflow.com/questions/8179399/javax-xml-parsers-saxparserfactory-classcastexception
})
public class AbstractTest extends PowerMockTestCase {
private Injector injector; // Google Guice injector
private ServletContext servletContext;
// required by PowerMock
@ObjectFactory
public IObjectFactory getObjectFactory() {
return new org.powermock.modules.testng.PowerMockObjectFactory();
}
protected ServletContext setupServletContext() {
if (servletContext == null) {
servletContext = Mockito.mock(ServletContext.class); // BANG here!!!
Mockito.doReturn(injector).when(servletContext).getAttribute(Mockito.eq(Injector.class.getName()));
}
return servletContext;
}
}
@PrepareForTest({FileUtils.class}) // the commons-io class, removing this annotation fixed the error
public class MyTest extends AbstractTest {
@BeforeMethod
protected void setupMocks() throws Exception {
// setup an injector
ServletContext ctx = setupServletContext();
}
}
What go exactly wrong here?
Thanks