Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 617
Siddharth Avatar asked Jul 30 '16 23:07

Siddharth


People also ask

How do I resolve Java Lang ExceptionInInitializerError in JUnit?

We can resolve the java. lang. ExceptionInInitializerError by ensuring that static initializer block of classes does not throw any Runtime Exception. We can resolve also resolve this exception by ensuring that the initializing static variable of classes also doesn't throw any Runtime Exception.

How do you handle Java Lang ExceptionInInitializerError?

How to handle the ExceptionInInitializerError Error. To avoid this error, simply ensure that: static initializers of classes do not throw any unchecked exception, and that. static class variable initializations do not throw any unchecked exceptions.

When should I use PowerMock?

The main aim of PowerMock is to extend the existing APIs with some methods and annotations to provide extra features that make unit testing quite easy. The PowerMock framework provides a class called PowerMockito used to create mock objects and initiates verification and expectation.


1 Answers

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

like image 167
Arthur Zagretdinov Avatar answered Oct 18 '22 11:10

Arthur Zagretdinov