tiebin-zhang / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 0 forks source link

NPE in PowerMockJUnit47RunnerDelegateImpl when using a test rule that throws exception #311

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. See attached test files.
2. (Make sure test.util.ThingToTest.getName() is returning a constant string 
and not calling a method on the mocked object.)
3. Run PowerMockFailingTest as normal.

What is the expected output? What do you see instead?
Expected output is a normal EasyMock 
java.lang.AssertionError: 
  Expectation failure on verify: simpleThingMock.getThingName: expected(1) actual(0)

But I got:
java.lang.RuntimeException: PowerMock internal error: Should never throw 
exception at this level
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.handleException(PowerMockJUnit44RunnerDelegateImpl.java:377)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:89)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:297)
    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:222)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:161)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:135)
    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:133)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:112)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
    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 org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.handleException(PowerMockJUnit44RunnerDelegateImpl.java:355)

What version of the product are you using? On what operating system?

PowerMock 1.4.7
EasyMock 3.0
JUnit 4.8.1
JDK1.6.0_21
Microsoft Windows XP Pro SP3

Please provide any additional information below.
I ran the test in Eclipse Helios.  I am using Maven.  

The problem appears to be in 
org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl.Po
werMockJUnit47MethodRunner.executeTest(Method, Object, Runnable), line 89.  The 
call to super.handleException gets made with a null potentialTestFailure 
variable.  I used the Eclipse debugger to replace the null with the caught 
exception and got my expected output.  I wondered if instead of calling 
super.handleException at line 89, code should be calling 
handleException(testMethod, e).

Original issue reported on code.google.com by laura.r....@gmail.com on 2 Feb 2011 at 9:14

Attachments:

GoogleCodeExporter commented 9 years ago
Still a problem as of 1.4.8 as I also ran into this issue, and debugged it to 
the same location as reporter:

org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl.Po
werMockJUnit47MethodRunner.executeTest(final Method method, final Object 
testInstance, final Runnable test):89

Debugging shows the Throwable e on line 84 is the exception I would expect to 
have seen being passed to the handleException method, not the null field 
potentialTestFailure.

Configuration:

PowerMock 1.4.8
EasyMock 3.0
JUnit 4.8.2
JDK 1.5 & 1.6
Mac OSX 10.5
run via Eclipse Helios and custom ant build script

Original comment by NathanDM...@gmail.com on 10 Mar 2011 at 11:32

GoogleCodeExporter commented 9 years ago
Unfortunatley I haven't had a change to look into this yet so it should still 
be present in 1.4.8. Thanks for the supplied examples though, it's really 
helpful.

Original comment by johan.ha...@gmail.com on 18 Mar 2011 at 10:18

GoogleCodeExporter commented 9 years ago
I've finally managed to find time to have a look at this. The problem was that 
the exception passed to super.handleException was potentialTestFailure even 
though it was null. When this is resolved the test you provide fails with the 
correct error message. It's how ever really difficult to write a test case to 
verify that this is solved :).

Anyway thanks a lot for your detailed description and your supplied code. 
Without it it would have been really hard to fix the issue. If you have time 
please try to verify that the fix actually works by checking out and building 
PowerMock from trunk. Thanks for your patience.

Original comment by johan.ha...@gmail.com on 5 Apr 2011 at 7:54