Saturday, May 18, 2013

Transition from Mockito to PowerMock

There was an error throwing out when I change the mocking framework from Mockito to PowerMock. See this error stack:

java.lang.ExceptionInInitializerError
 at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.(ConditionalStackTraceFilter.java:17)
 at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:30)
 at org.mockito.exceptions.base.MockitoException.(MockitoException.java:19)
 at org.mockito.exceptions.misusing.MockitoConfigurationException.(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.(ClassPathLoader.java:59)
 at org.mockito.internal.util.MockUtil.(MockUtil.java:21)
 at org.mockito.internal.MockitoCore.(MockitoCore.java:40)
 at org.mockito.Mockito.(Mockito.java:932)
 at org.huahsin.BuilderTest.(BuilderTest.java:41)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
 at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.createTestInstance(PowerMockJUnit44RunnerDelegateImpl.java:188)
 at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.createTest(PowerMockJUnit44RunnerDelegateImpl.java:173)
 at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:195)
 at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:148)
 at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
 at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
 at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
 at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
 at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:101)
 at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
 at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
 at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.NullPointerException
 at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.(StackTraceFilter.java:21)
 ... 33 more

I don’t know where this stupid NullPointerException came from? The java.lang.ExceptionInInitializerError is real misleading, and I was being fool with this stupid error. If I am able to realize that MyTestClass has implement PowerMock framework already, then the mock implementation should also has to change accordingly. The actual problem on this error is that I was mess up the mock framework where I suppose to use the PowerMock’s API instead of Mockito’s API.

This is the code that causes the error shown above.

@RunWith(PowerMockRunner.class)
@PrepareForTest(CommonDao.class)
public class MyTestClass {

   private static TheMockClass theMockClass = Mockito.mock(TheMockClass.class);
}

To resolve this issue, use PowerMock.mock(TheMockClass.class); when initialize a mock object.

No comments: