Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerMockito (with Mockito) failing with ExceptionInInitializerError

We are using Powermockito with Mockito to mock some static classes. There seems to be java.lang.ExceptionInInitializerError thrown every time.

Can you help me identify where the problem is?

Java class under test

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;
    }
}

Junit test with Powermock runner

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);
    }
}

That's it. The test code does not work as soon as I try to mock MultiMatchQueryBuilder.

This is the Exception:

java.lang.ExceptionInInitializerError 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) at 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) at sun.reflect.DelegatingMethodAccessorImpl.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) at org.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) Caused by: java.lang.NullPointerException at org.elasticsearch.Build.(Build.java:47) ... 41 more

Process finished with exit code 255

Note:

The source code of actual underlying elasticsearch classes can be found here

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

and

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

like image 940
Vinoth Kumar C M Avatar asked Aug 16 '17 10:08

Vinoth Kumar C M


People also ask

What is the difference between Mockito and PowerMockito?

While Mockito can help with test case writing, there are certain things it cannot do viz:. mocking or testing private, final or static methods. That is where, PowerMockito comes to the rescue. PowerMockito is capable of testing private, final or static methods as it makes use of Java Reflection API.

Can we use PowerMock and Mockito together?

Mockito allows us to create mock objects. Since static method belongs to the class, there is no way in Mockito to mock static methods. However, we can use PowerMock along with Mockito framework to mock static methods.

Is Mockito PowerMock Which is better?

The division of work between the two is that Mockito is kind of good for all the standard cases while PowerMock is needed for the harder cases. That includes for example mocking static and private methods.

What does PowerMockito Mockstatic do?

PowerMockito is a PowerMock's extension API to support Mockito. It provides capabilities to work with the Java Reflection API in a simple way to overcome the problems of Mockito, such as the lack of ability to mock final, static or private methods.


1 Answers

When calling with mocks org.elasticsearch.Build#getElasticsearchCodebase

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

returns null because the code has no location (Dynamic method generated by cglib.)

So when initializing org.elasticsearch.Build during your mock code using

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

Of course, the mock will not success and throw ExceptionInInitializerError which indicates an exception occurred during evaluation of a static initializer or the initializer for a static variable.


You can easily reproduce this exception using following code:

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

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

}
like image 90
aristotll Avatar answered Sep 21 '22 13:09

aristotll