Test should never throw an exception to this level

5,601 views
Skip to first unread message

mt17

unread,
Dec 8, 2008, 12:16:28 PM12/8/08
to PowerMock
Hej,

jag har ett återkommande problem som jag slitit håret över många
gånger nu... Jag skriver tester i Eclipse och mockar mha PowerMock.
Testerna går igenom och allt är frid och fröjd men när jag försöker
bygga projektet i maven får jag:

org.apache.maven.surefire.booter.SurefireExecutionException: test
should never throw an exception to this level; nested exception is
java.lang.RuntimeException: test should never throw an exception to
this level
java.lang.RuntimeException: test should never throw an exception to
this level
at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters
(MethodRoadie.java:97)
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:200)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods
(PowerMockJUnit44RunnerDelegateImpl.java:150)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$1.run(PowerMockJUnit44RunnerDelegateImpl.java:124)
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:122)
at
org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run
(JUnit4TestSuiteChunkerImpl.java:89)
at
org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run
(AbstractCommonPowerMockRunner.java:44)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute
(JUnit4TestSet.java:62)
at
org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet
(AbstractDirectoryTestSuite.java:140)
at
org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute
(AbstractDirectoryTestSuite.java:165)
at org.apache.maven.surefire.Surefire.run(Surefire.java:107)
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.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess
(SurefireBooter.java:289)
at org.apache.maven.surefire.booter.SurefireBooter.main
(SurefireBooter.java:993)

Det verkar som kan vara när jag försöker köra
Whitebox.setInternalState eller PowerMock.suppressMethodCode inuti en
testmetod och inte i en @Before-metod. Ibland får jag det också då jag
försöker använda expectNew på en klass. Framförallt expectNew måste ju
gå att köra i en vanlig testmetod?

Jag har provat att sätta try-catcher överallt istället för att
testerna ska kasta exceptions men det hjälper inte.

Kan tyvärr inte bifoga koden men hoppas detta är något ni stött på
någon gång och har något tips...

MVH
Magnus

Johan Haleby

unread,
Dec 9, 2008, 1:16:02 AM12/9/08
to powe...@googlegroups.com
I've experienced this a couple of times but it's always related to something else, never the less I think the error message is right, it should never happen :). Does the test work in Eclipse or does it fail there as well? In that case could you set a break point in class MethodRoadie in JUnit4 where indicated below:
   public void runBeforesThenTestThenAfters(Runnable test) {
      try {
         runBefores();
         test.run();
      } catch (FailedBefore e) {
      } catch (Exception e) {
         throw new RuntimeException("test should never throw an exception to this level");  // set a breakpoint here.
      } finally {
         runAfters();
      }     
   }

and check what the real exception and its stacktrace is (i.e. "e"). Could you do this and the post the stacktrace here and I'll see what's going on. It might be a bug.

/Johan

manuel fdez

unread,
Dec 9, 2008, 5:48:54 AM12/9/08
to PowerMock
Hi everybody.

I'm having this error too.

This piece of code works well, as my "ExpansionException" must be
thrown when calling "removeStopWords":

@Test(expected = ExpansionException.class)
public void testRemoveStopWords3() throws Exception {

Query q1 = new ElementQuery("with");
Query q2 = new ElementQuery("of");
Query testQuery = new BinaryQuery(Arrays.asList(q1, q2),
BinaryOperator.ANDCrisp);
testQuery.removeStopWords("eng");
}

But, with PowerMock ("SemanticServices.isStopWord" is used inside
"removeStopWords"):

@PrepareForTest(SemanticServices.class)
@Test(expected = ExpansionException.class)
public void testRemoveStopWords3() throws Exception {

PowerMock.mockStaticPartial(SemanticServices.class, "isStopWord");
EasyMock.expect(SemanticServices.isStopWord("with", "eng")).andReturn
(true);
EasyMock.expect(SemanticServices.isStopWord("of", "eng")).andReturn
(true);
PowerMock.replay(SemanticServices.class);

Query q1 = new ElementQuery("with");
Query q2 = new ElementQuery("of");
Query testQuery = new BinaryQuery(Arrays.asList(q1, q2),
BinaryOperator.ANDCrisp);
testQuery.removeStopWords("eng");
}

The result is:
java.lang.RuntimeException: test should never throw an exception to
this level
at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters
(MethodRoadie.java:97)
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:200)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods
(PowerMockJUnit44RunnerDelegateImpl.java:150)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl
$1.run(PowerMockJUnit44RunnerDelegateImpl.java:124)
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:122)
at
org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run
(JUnit4TestSuiteChunkerImpl.java:89)
at
org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run
(AbstractCommonPowerMockRunner.java:44)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run
(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run
(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run
(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main
(RemoteTestRunner.java:196)

Hope this can be useful. Thanks.

On Dec 9, 7:16 am, "Johan Haleby" <johan.hal...@gmail.com> wrote:
> I've experienced this a couple of times but it's always related to something
> else, never the less I think the error message is right, it should never
> happen :). Does the test work in Eclipse or does it fail there as well? In
> that case could you set a break point in class MethodRoadie in JUnit4 where
> indicated below:
>    public void runBeforesThenTestThenAfters(Runnable test) {
>       try {
>          runBefores();
>          test.run();
>       } catch (FailedBefore e) {
>       } catch (Exception e) {
>          throw new RuntimeException("test should never throw an exception to
> this level");  // set a breakpoint here.
>       } finally {
>          runAfters();
>       }
>    }
>
> and check what the real exception and its stacktrace is (i.e. "e"). Could
> you do this and the post the stacktrace here and I'll see what's going on.
> It might be a bug.
>
> /Johan
>

Johan Haleby

unread,
Dec 9, 2008, 6:34:16 AM12/9/08
to powe...@googlegroups.com
Is it possible for you to attach the java files so that I can try it out myself? Unfortunately JUnit never propagates the real cause of the exception.

Also you could try placing the @PrepareForTest annotation at the class-level of the test case instead of the method-level to see if it works better.

mt17

unread,
Dec 9, 2008, 6:46:46 AM12/9/08
to PowerMock
This doesn't happen in Eclipse, only when building with maven. (I have
the @PrepareForTest annotation at the class-level.)

I found out that I could do remote debugging on the maven test though,
when doing that I see that the caught exception in MethodRoadie is:

e RuntimeException (id=86)
cause NoClassDefFoundError (id=83)
cause ClassNotFoundException (id=94)
cause null
detailMessage "org.junit.Assume
$AssumptionViolatedException" (id=97)
ex null
stackTrace null
detailMessage "org/junit/Assume
$AssumptionViolatedException" (id=96)
stackTrace null
detailMessage "Internal error in PowerMock." (id=90)
stackTrace null

So the stackTrace is pretty useless but it seems that the
AssumptionViolatedException class is missing.

Googling for org.junit.Assume$AssumptionViolatedException I found:
http://jira.springframework.org/browse/SPR-5145
which seems to say that this is an issue with junit4.5.

(I also found that the org.junit.internal.runners.MethodRoadie class
is deprecated by the way.)

Well I don't know if this is a bug in PowerMock or JUnit or just in
our setup, I would appreciate any feedback though!

/Magnus

Johan Haleby

unread,
Dec 9, 2008, 7:31:46 AM12/9/08
to powe...@googlegroups.com
Thanks for the info, the stack trace is not all the useless but I cannot see any obvious solution or even where exactly the error happened. We've mainly used PowerMock with JUnit 4.4 and our runner is built for this version but all our own tests worked out of the box with JUnit 4.5 as well and I still haven't experienced any problems myself. But you're right, the MethodRoadie is deprecated since JUnit 4.5 so we definatley need to do something about that til the next version of JUnit. Are you sure you're using JUnit 4.5 in Eclipse as well? Is there any chance that you can supply a code snippet so that I can try to reproduce the error?

Could you send me the full stack-trace so I know where PowerMock says "Internal error in PowerMock."? It may be a bug in JUnit 4.5 just as the Spring guys points out but perhaps we could create some sort of work-around until we've created a new PowerMockRunner built for JUnit 4.5+ (this will require much work though).

/Johan

Manuel Fernandez Sanchez de la Blanca

unread,
Dec 9, 2008, 7:48:05 AM12/9/08
to powe...@googlegroups.com
Hi.

Unfortunately It's not possible for me to attach the java files, but I've created a simple example showing this issue.

thanks.

2008/12/9 Johan Haleby <johan....@gmail.com>



--
Manuel Fernández Sánchez de la Blanca
myExample.zip

Johan Haleby

unread,
Dec 9, 2008, 8:44:07 AM12/9/08
to powe...@googlegroups.com
I've managed to reproduce the error and I'll see what I can do to fix it, thank you very much!

mt17

unread,
Dec 9, 2008, 9:05:49 AM12/9/08
to PowerMock
yeah sorry but I can't publish the code either.

My eclipse is running JUnit4_4.3.1 so that's probably why it works in
Eclipse but not in maven (which is using JUnit4.5)

About the full stack trace I don't really know what you mean, the
information I sent before is the information Eclipse gives me about
the exception that is caught in MethodRoadie. It says that the stack
trace is null. The stack trace I get when continuing is the trace I
wrote in the first post.

Nice to know that you are working on it! At the moment I'll just have
to speak with the responsible people here and try to convince them
that we should use JUnit4.4 instead ;)

//Magnus

Johan Haleby

unread,
Dec 9, 2008, 9:15:19 AM12/9/08
to powe...@googlegroups.com
It's quite possible that it's related to the same issue. JUnit 4.5 doesn't seem to be 100% backward compatible with JUnit 4.4 since they moved the AssumptionViolatedException. But I'm working on it as we speak.

Johan Haleby

unread,
Dec 10, 2008, 4:09:54 AM12/10/08
to powe...@googlegroups.com
I believe that this issue has now been fixed and I've committed the changes to trunk. If you like you could try checking out and build the project from our subversion repository (using mvn install), it would be great help for us if you could try it out to make sure that it _really_ do work for you. If not we're going to release 1.1 any day now which will include this fix.

/Johan

mt17

unread,
Dec 12, 2008, 9:08:58 AM12/12/08
to PowerMock
Thanks for the help.

Spoke to the architect earlier and he tried to install 1.1 in our
project but there was some problems.. He's going to experiment a bit
before we can try it for real so for the moment we will use JUnit4.4
to make it work.

On 10 Dec, 10:09, "Johan Haleby" <johan.hal...@gmail.com> wrote:
> I believe that this issue has now been fixed and I've committed the changes
> to trunk. If you like you could try checking out and build the project from
> our subversion repository (using mvn install), it would be great help for us
> if you could try it out to make sure that it _really_ do work for you. If
> not we're going to release 1.1 any day now which will include this fix.
>
> /Johan
>
> On Tue, Dec 9, 2008 at 3:15 PM, Johan Haleby <johan.hal...@gmail.com> wrote:
> > It's quite possible that it's related to the same issue. JUnit 4.5 doesn't
> > seem to be 100% backward compatible with JUnit 4.4 since they moved the
> > AssumptionViolatedException. But I'm working on it as we speak.
>

Johan Haleby

unread,
Dec 12, 2008, 9:18:36 AM12/12/08
to powe...@googlegroups.com
Was it a problem related to the PowerMock 1.1 release or was it some other kind of problem? If it was a problem with PowerMock we'd love to know so that we can fix it :)
Reply all
Reply to author
Forward
0 new messages