PowerMockito(与Mockito)失败,异常初始化器错误

2022-09-03 16:05:28

我们正在将Powermockito与Mockito一起使用来模拟一些静态类。似乎每次都有抛出。java.lang.ExceptionInInitializerError

你们能帮我确定问题出在哪里吗?

测试中的 Java 类

package com.myproject.myproduct.search.domain;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

public class MyQueryBuilder {

    public MultiMatchQueryBuilder getMultiMatchQueryBuilder() {
        MultiMatchQueryBuilder builder = QueryBuilders.multiMatchQuery("term", "field1");
        builder.field("field1",200.9f);
        return builder;
    }
}

使用 Powermock 流道进行 Junit 测试

package com.myproject.myproduct.search.domain;

import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(QueryBuilders.class)
public class MyQueryBuilderTest {

    private MyQueryBuilder myQueryBuilder;

    @Test
    public void test() {
        PowerMockito.mockStatic(QueryBuilders.class);
        MultiMatchQueryBuilder builder = PowerMockito.mock(MultiMatchQueryBuilder.class);
    }
}

就是这样。一旦我尝试模拟MultiMatchQueryBuilder,测试代码就不起作用。

这是例外:

java.lang.ExceptionInitializerError at org.elasticsearch.common.logging.DeprecationLogger.(DeprecationLogger.java:138) at org.elasticsearch.common.ParseField.(ParseField.java:35) at org.elasticsearch.index.query.AbstractQueryBuilder.(AbstractQueryBuilder.java:53) at sun.reflect.GeneratedSerializationConstructorAccessor7.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:40) at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:59) at org.mockito.internal.creation.jmock.ClassImposterizer.createProxy(ClassImposterizer.java:128) at org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:63) at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:111) at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:60) at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:143) atcom.spartasystems.stratas.search.domain.MyQueryBuilderTest.testBoostSetProperly(MyQueryBuilderTest.java:22) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.reflectingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310) at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:88) at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82) atorg.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282) at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86) at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120) at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33) at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122) at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:104) at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53) at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53) at org.junit.runner.JUnitCore.run(JUnitCore.java:160) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Cause by: java.lang.NullPointerException at org.elasticsearch.Build.(建造.java:47) ...另外 41

进程已完成,退出代码为 255

注意:

实际的底层弹性搜索类的源代码可以在这里找到

https://github.com/elastic/elasticsearch/blob/master/core/src/main/java/org/elasticsearch/index/query/QueryBuilders.java

https://github.com/elastic/elasticsearch/blob/master/core/src/main/java/org/elasticsearch/index/query/MultiMatchQueryBuilder.java


答案 1

当使用 mocks org.elasticsearch.Build#getElasticsearchCodebase 调用时

Build.class.getProtectionDomain().getCodeSource().getLocation()

返回,因为代码没有位置(由 cglib 生成的动态方法。null

因此,在初始化 org.elasticsearch.Build 期间,请使用

final URL url = getElasticsearchCodebase(); // url is null
final String urlStr = url.toString(); // null pointer exception.

当然,模拟不会成功并引发,这表示在评估静态初始值设定项或静态变量的初始值设定项期间发生异常。ExceptionInInitializerError


您可以使用以下代码轻松重现此异常:

@RunWith(PowerMockRunner.class)
@PrepareForTest({QueryBuilders.class})
public class MyQueryBuilderTest {

    @Test
    public void test() {
        final Build current = Build.CURRENT;
    }

}

答案 2

推荐