Siddharth
Siddharth

Reputation: 83

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.

Upvotes: 5

Views: 22906

Answers (2)

Nathaniel Ruiz
Nathaniel Ruiz

Reputation: 617

If you're getting this error on VSCode, this is because your VSCode is on a newer Java version.

You can open the Command Palette with CMD + Shift + P and search Java: Configure Java Runtime.

From there you can pick an older Java version, for example Java 8 instead of Java 17. My Java 8 was located at /Library/Java/JavaVirtualMachines/amazon-corretto-8.jdk/Contents/Home.

Upvotes: 0

Artur Zagretdinov
Artur Zagretdinov

Reputation: 2064

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

Upvotes: 16

Related Questions