PowerMock Mockito [PowerMockito] @PrepareForTest -> java.lang.NoClassDefFoundError: javassist/NotFoundException

StackOverflow https://stackoverflow.com/questions/23157279

Question

I'm trying to use my 1st PowerMockito [PowerMock + Mockito] mock in an existing JUnit test.

I've narrowed down that when I simply have the presence of the class-level annotation @PrepareForTest (along with @RunWith(PowerMockRunner.class), but not any actual mocking code), I get java.lang.NoClassDefFoundError: javassist/NotFoundException: If I comment out the @PrepareForTest, and also any actual mocking code to eliminate confounding variables, that error does not happen; if I UN-comment just the @PrepareForTest, then the error happens. So I believe that narrows the problem down to just to do with trying to use the annotation...

I have these jars on my buildpath: junit-4.11.jaar, junit-addons-1.4.jar, mockito-all-1.9.5.jar, mockito-all-1.9.5-sources.jar, powermock-mockito-1.5.4-full.jar.

The powermock-mockito-1.5.4-full.jar is the only new one I added to try out PowerMockito (from https://code.google.com/p/powermock/wiki/Downloads?tm=2): It gives me everything additional I need to write the new PowerMockito mocking code without Eclipse compile errors. (I already had the other mentioned ars from previous existing JUnit & Mockito [alone, no PowerMockito] tests),

What could I be doing wrong to get that error?

Was it helpful?

Solution

(This pretty much helped me figure it out)

Pretty simple solution (I almost had it right to begin with): I was missing a few PowerMockito dependency jars (from https://code.google.com/p/powermock/wiki/Downloads?tm=2): Specifically {cglib-nodep-2.2.2.jar, javssist-3.18.1-GA.jar, objenesis-2.1.jar}.

It was a simple solution and kinda dumb mistake to begin with in the 1st place, since that powerMock googleCode download page has all of them: At 1st, when I had the problem, I had only downloaded and added powermock-mockito-1.5.4-full.jar; I already had the JUnit & Mockito jars, so I think that set of 3 let me write PowerMock code without any compile errors in Eclipse. The googlecode download page has the powermockito jar + dependecies in a zip, but also said powerockito jar available alone too. That situation made it kinda hard to realize I was missing some others since it appeared I had all I need from that perspective (thought I already had the dependencies I needed per my existing JUnit and Mockito jars).

In actuality, the zip with dependencies had the additional ones I needed to solve the problem. I should have double-checked that 1st, but didn't realize I made the opposite bad assumption.

So... answer is to make sure to get the zip with all dependencies [and add them all to your buildpath]. Kinda obvious and dumb in my part in the end, but hey, a lot of software bugs and problems end up being that: Simple bad assumptions or overlooked details we don't realize we made, and therefore take a while to realize when troubleshooting...

OTHER TIPS

We were getting this with one of our ant junit targets that was using PowerMock 1.5.5. The issue, in our case, was the wrong version of javassist was on the ant junit task classpath.

PowerMock 1.5.5, according to this link from the mvn rep, has a dependency on javassist 3.18.2-GA

Once we fixed this.. all our mocking fun began!

I was lately trying to run Mockito examples and failing to supply all the Jar files.

This link contains links for necessary binaries.

https://github.com/jayway/powermock/wiki/Downloads

Use below for Mockito and PowerMock combination.

powermock-mockito-junit-1.6.3.jar

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top