我们正在使用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.ExceptionInInitializerError at org.elasticsearch.common.logging.DeprecationLogger。(DeprecationLogger.java:138) 在org.elasticsearch.common.ParseField。(ParseField.java:35) 在 org.elasticsearch.index.query.AbstractQueryBuilder。(AbstractQueryBuilder.java:53) 在 sun.reflect.GeneratedSerializationConstructorAccessor7.newInstance(未知 来源)at java.lang.reflect.Constructor.newInstance(Constructor.java:423)at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:40) 在org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:59)at org.mockito.internal.creation.jmock.ClassImposterizer.createProxy(ClassImposterizer.java:128) 在 org.mockito.internal.creation.jmock.ClassImposterizer.imposterise(ClassImposterizer.java:63) 在 org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:111) 在 org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:60) 在org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:143) 在 com.spartasystems.stratas.search.domain.MyQueryBuilderTest.testBoostSetProperly(MyQueryBuilderTest.java:22) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在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) 在 org.junit.internal.runners.MethodRoadie $ 2.run(MethodRoadie.java:88) 在 org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:96) 在 org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl $ PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294) 在 org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl $ PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127) 在 org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl $ PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82) 在 org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl $ PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282) 在 org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86) 在org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49) 在 org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207) 在 org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146) 在 org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl $ 1.run(PowerMockJUnit44RunnerDelegateImpl.java:120) 在 org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33) 在 org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45) 在 org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122) 在 org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:104) 在 org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53) 在 org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53) 在org.junit.runner.JUnitCore.run(JUnitCore.java:160)at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) 在 com.intellij.rt.execution.junit.IdeaTestRunner $ Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) 在 com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) 在 com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) 引起:java.lang.NullPointerException at org.elasticsearch.Build。(Build.java:47)... 41 more
处理完成,退出代码为255
注意:
可以在此处找到实际基础弹性搜索类的源代码
和
答案 0 :(得分:3)
使用模拟org.elasticsearch.Build#getElasticsearchCodebase
Build.class.getProtectionDomain().getCodeSource().getLocation()
返回null
,因为代码没有位置(cglib生成的动态方法。)
因此,在使用
的模拟代码中initializingorg.elasticsearch.Build
时
final URL url = getElasticsearchCodebase(); // url is null
final String urlStr = url.toString(); // null pointer exception.
当然,mock不会成功并抛出ExceptionInInitializerError
,这表示在评估静态初始值设定项或静态变量的初始值设定项时发生了异常。
您可以使用以下代码轻松重现此异常:
@RunWith(PowerMockRunner.class)
@PrepareForTest({QueryBuilders.class})
public class MyQueryBuilderTest {
@Test
public void test() {
final Build current = Build.CURRENT;
}
}