5
votes

I am trying to mock static method using EasyMock+PowerMock. If I dont mock the static method, then I get the exception java.lang.ExceptionInInitializerError but with a different stack trace which is purely due to my code files and the error is obvious. However, if I mock the static method using EasyMock+PowerMock, the line PowerMock.mockStaticNice(Classname.class) throws the same exception but with a different stack trace. The stack trace is:

 java.lang.ExceptionInInitializerError
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:348)
        at net.sf.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:386)
        at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:219)
        at net.sf.cglib.proxy.Enhancer.createHelper(Enhancer.java:377)
        at net.sf.cglib.proxy.Enhancer.createClass(Enhancer.java:317)
        at org.easymock.internal.ClassProxyFactory.createProxy(ClassProxyFactory.java:175)
        at org.easymock.internal.MocksControl.createMock(MocksControl.java:114)
        at org.easymock.internal.MocksControl.createMock(MocksControl.java:88)
        at org.easymock.internal.MocksControl.createMock(MocksControl.java:79)
        at org.powermock.api.easymock.PowerMock.doCreateMock(PowerMock.java:2212)
        at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:2163)
        at org.powermock.api.easymock.PowerMock.mockStaticNice(PowerMock.java:331)
        at PackageName(ClassName.java:125)
............................



The line 125 is PowerMock.mockStaticNice(Classname.class)

I have already tried this:
1) Mention class name containing static method in PrepareForTest({class1.class, class2.class, class3.class})
2) Mock static methods in @Before annotation.

I am stuck with this problem for the last 2 days. Kindly suggest solutions.

1
Just for the record: you understand that static is an abnormality that simply should be avoided in good OO designs? And surprise: no static calls, no need for PowerMock. - GhostCat
I understand that. But I am just editing someone's previous code, so I don't have any option apart from mocking the static method. - Siddharth
Well, you could add an interface/wrapper class and change the static call to use that wrapper; but of course; that carries some risk; and in that case, it is probably not worth the effort. In that sense: I rarely see answers here that really say "this is how you get it to work with powermock"; so maybe you are better off posting to the Powermock google group: groups.google.com/forum/#!forum/powermock - GhostCat
Thanks! I will post the doubt in that google group. Adding wrapper class is not possible because I am not allowed to change previously written code. - Siddharth

1 Answers

15
votes

As I understood from your explanation the ExceptionInInitializerError is thrown during static initialisation of class? I've made such conclusion, because according to stacktrace the line PowerMock.mockStaticNice(Classname.class) is a first place where the class Classname is being loaded.

In this case you have to use @SuppressStaticInitializationFor(PackageName.ClassName`). More information you may find in PowerMock documentation: Suppress Unwanted Behavior