File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Testing and the fly likes PowerMockito + Junit + Ant Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "PowerMockito + Junit + Ant" Watch "PowerMockito + Junit + Ant" New topic

PowerMockito + Junit + Ant

Seema Kekre
Ranch Hand

Joined: Dec 02, 2010
Posts: 35
I am new to Powermockito. I have a Junit4 setup with Ant1.7.1. I am mocking a final class using mockito 1.8.5

When I run this test using Eclipse, it runs fine. When I run the test using my ant build I get the following error. From this error it looks like it can find Powermockito and mockito in its classpath and there is just one mockito jar in the lib (claspath).

[junit] java.lang.ExceptionInInitializerError
[junit] at java.lang.Class.forName0(Native Method)
[junit] at java.lang.Class.forName(
[junit] Caused by: java.lang.IllegalArgumentException: Cannot subclass final class class
[junit] at org.mockito.cglib.proxy.Enhancer.generateClass(
[junit] at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(
[junit] at org.mockito.cglib.core.AbstractClassGenerator.create(
[junit] at org.mockito.cglib.proxy.Enhancer.createHelper(
[junit] at org.mockito.cglib.proxy.Enhancer.createClass(
[junit] at org.mockito.internal.creation.jmock.ClassImposterizer.createProxyClass(
[junit] at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(
[junit] at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(
[junit] at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(
[junit] at org.powermock.api.mockito.PowerMockito.mock(
[junit] at

My eclipse and Ant are pointing to the same junit. I dont think this is a Junit issue, but a mockito issue but I cannot figure out how to start figuring out the issue.

Why does it work with Eclipse and not with Ant? How can I figure out the order in which the Classloaders are loaded?

Thanks in advance.

Seema Kekre
Ranch Hand

Joined: Dec 02, 2010
Posts: 35
Was able to figure it out. For some reason, using mock for final classes in static causes a java.lang.ExceptionInInitializerError. When I changed this to a class method it still throws this Exception but my tests pass. Will post on Powermockito forum to understand this better.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

using mockito we cannt mock the final classes but if you are using powermockito then it can be done..Just put the powermockit jar in the classpath and remove mockito jar

SCJP6.0,My blog Ranchers from Delhi
I agree. Here's the link:
subject: PowerMockito + Junit + Ant
It's not a secret anymore!