PowerMock+Mockito initialization errors

6,394 views
Skip to first unread message

RealObject

unread,
May 22, 2013, 7:50:37 PM5/22/13
to powe...@googlegroups.com
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


Johan Haleby

unread,
May 23, 2013, 2:25:56 AM5/23/13
to powe...@googlegroups.com
What combination of PowerMock and Mockito are you using?

Regards
/Johan





--
You received this message because you are subscribed to the Google Groups "PowerMock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to powermock+...@googlegroups.com.
To post to this group, send email to powe...@googlegroups.com.
Visit this group at http://groups.google.com/group/powermock?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

RealObject

unread,
May 23, 2013, 1:32:23 PM5/23/13
to powe...@googlegroups.com
Here's what the maven pom.xml has

  <properties>
    <powermock.version>1.5</powermock.version>
  </properties>

    <dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.8</version>
      <scope>test</scope>
    </dependency>

    <!-- Mockito for test mocking -->
    <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-all</artifactId>
      <version>1.9.5</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-module-testng</artifactId>
      <version>${powermock.version}</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.powermock</groupId>
      <artifactId>powermock-api-mockito</artifactId>
      <version>${powermock.version}</version>
      <scope>test</scope>
    </dependency>

Johan Haleby

unread,
May 23, 2013, 2:19:09 PM5/23/13
to powe...@googlegroups.com
Alright, I can't really tell what's going on. I see that you have a lot of classes you ignore and that may be what's causing it as a side effect. If you need to do a lot of ignores then I would try the java agent bootstrapper.

Regards,
/Johan

István Kovács

unread,
May 27, 2013, 4:12:35 AM5/27/13
to powe...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages