使用 EasyMock+PowerMock 模拟静态方法时出现 java.lang.ExceptionInInitializerError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38679426/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 03:39:48  来源:igfitidea点击:

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

javaunit-testingstatic-methodspowermockeasymock

提问by Siddharth

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:

我正在尝试使用 EasyMock+PowerMock 模拟静态方法。如果我不模拟静态方法,那么我会得到异常 java.lang.ExceptionInInitializerError 但具有不同的堆栈跟踪,这纯粹是由于我的代码文件造成的,并且错误很明显。但是,如果我使用 EasyMock+PowerMock 模拟静态方法,则 PowerMock.mockStaticNice(Classname.class) 行会抛出相同的异常,但具有不同的堆栈跟踪。堆栈跟踪是:

 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.



第 125 行是 PowerMock.mockStaticNice(Classname.class)

我已经试过了:
1) 在 PrepareForTest({class1.class, class2.class, class3.class}) 中提及包含静态方法的类名
2) 在@中模拟静态方法注释前。

在过去的 2 天里,我一直被这个问题困扰。请提出解决方案。

回答by Arthur Zagretdinov

As I understood from your explanation the ExceptionInInitializerErroris 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 Classnameis being loaded.

正如我从你的解释中了解到的,ExceptionInInitializerError在类的静态初始化期间抛出了?我做出了这样的结论,因为根据堆栈跟踪,该行PowerMock.mockStaticNice(Classname.class)Classname加载类的第一个位置。

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

在这种情况下,您必须使用@SuppressStaticInitializationFor(PackageName.ClassName`)。您可以在 PowerMock 文档中找到更多信息:Suppress Unwanted Behavior