Got ExceptionInInitializerError when mocking constructor of a class. How to fix it?

1,977 views
Skip to first unread message

Markey

unread,
Sep 28, 2011, 11:06:40 PM9/28/11
to PowerMock
Hi,

Here is my case. I have a AbstractController class. It has a sub class
Controller. In one of AbstractController's methods a new
ApplicationLock is instantiated. I'd like to mock ApplicationLock. I
wrote a test case like below.

@test
public void testMethod(){
ApplicationLock mockLock=PowerMockito.mock(ApplicationLock.class);
PowerMockito.when(mockLock.tryObtain()).thenReturn(true);

PowerMockito.whenNew(ApplicationLock.class).withArguments(argThat(new
IsFile()),anyString()).thenReturn(mockLock);
}

I've added necessary annotations to the test class.
@RunWith(PowerMockRunner.class)
@PrepareForTest({ApplicationLock.class})

But I got the following error when running this test case. That is a
static initializer in AbstractController.

Caused by: java.lang.NullPointerException
at
com.acompany.controller.common.AbstractController.<clinit>(AbstractController.java:
65)

private static final String DEFAULT_FOLDER =
AbstractController.class.getProtectionDomain().getCodeSource()
.getLocation().getPath();


Full stack trace is as below.

java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at javassist.runtime.Desc.getClassObject(Desc.java:44)
at javassist.runtime.Desc.getClassType(Desc.java:153)
at javassist.runtime.Desc.getType(Desc.java:123)
at javassist.runtime.Desc.getType(Desc.java:79)
at
com.acompany.controller.portfolio.ControllerTest.testIncrementalFail(ControllerTest.java:
195)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:
307)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:
86)
at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:
94)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:
294)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl
$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:
112)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl
$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:
73)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:
282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:
84)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:
207)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:
146)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
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:
118)
at
org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:
102)
at
org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:
53)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:
49)
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
com.acompany.controller.common.AbstractController.<clinit>(AbstractController.java:
65)
... 35 more


Johan Haleby

unread,
Sep 29, 2011, 2:40:31 AM9/29/11
to powe...@googlegroups.com
Hi, 

When mocking the construction of an object you don't prepare the class of the object to be constructed but the class constructing the new instance. See documentation here. Also I would probably not mock the construction of the mock but rather create a mock of the lock normally and simply overwrite the real lock by using Whitebox.setInternalState(testInstance, lockMock);

/Johan




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

Markey

unread,
Sep 29, 2011, 10:27:19 PM9/29/11
to PowerMock
Thanks, Johan.

I've noticed that. I've tried to prepare Controller.class, as well as
AbstractController. Still got the same exception.

Anyway, I am going to try your suggestion, the whitebox one. :)

On Sep 29, 2:40 pm, Johan Haleby <johan.hal...@gmail.com> wrote:
> Hi,
>
> When mocking the construction of an object you don't prepare the class of
> the object to be constructed but the class *constructing* the new instance.
> See documentation
> here<http://code.google.com/p/powermock/wiki/MockConstructor>.

Markey

unread,
Sep 29, 2011, 10:50:03 PM9/29/11
to PowerMock
Hi Johan,

Looks like it does not work for my case. My understanding is
Whitebox.setInternalState(testInstance, lockMock) is used to set a
member field of a class. But my case is like below. appLock is an
internal variable in one method.

public int start() {
ApplicationLock appLock = getApplicationLock();
if (!appLock.tryObtain()) {
err.println("Controller is already running. Couldn't start
one more instance.");
return 1;
}
...
}
protected ApplicationLock getApplicationLock() {
return new ApplicationLock(getAppFolder(), LOCK_FILE_NAME);
Reply all
Reply to author
Forward
0 new messages