samedi 30 juillet 2016

java.lang.ExceptionInInitializerError when mocking static method using EasyMock+PowerMock

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.

Aucun commentaire:

Enregistrer un commentaire