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

cellepo picture cellepo · Apr 18, 2014 · Viewed 21.2k times · Source

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?

Answer

cellepo picture cellepo · Apr 18, 2014

(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...