6

I'm using Mockito with PowerMock for the first time and I'm having an error when running the below code at the following line:

MockitoAnnotations.initMocks(SearchTest.class); 

The error is:

java.lang.ExceptionInInitializerError at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.<init>(ConditionalStackTraceFilter.java:17) at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:30) at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:19) at org.mockito.exceptions.misusing.MockitoConfigurationException.<init>(MockitoConfigurationException.java:18) at org.mockito.internal.configuration.ClassPathLoader.loadImplementations(ClassPathLoader.java:145) at org.mockito.internal.configuration.ClassPathLoader.findPluginImplementation(ClassPathLoader.java:110) at org.mockito.internal.configuration.ClassPathLoader.findPlatformMockMaker(ClassPathLoader.java:106) at org.mockito.internal.configuration.ClassPathLoader.<clinit>(ClassPathLoader.java:59) at org.mockito.internal.configuration.GlobalConfiguration.createConfig(GlobalConfiguration.java:38) at org.mockito.internal.configuration.GlobalConfiguration.<init>(GlobalConfiguration.java:32) at org.mockito.MockitoAnnotations.initMocks(MockitoAnnotations.java:94) Caused by: java.lang.NullPointerException 

The code for the test class is:

 import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized.Parameters; import org.mockito.Matchers; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(Parameterized.class) @PrepareForTest(InputStreamReader.class) public class SearchTest { private String preFile; private String expectedPreFile; private String postFile; private String expectedpostFile; @Parameters public static Collection<Object[]> data() { return Arrays.asList(new Object[][] { { "test1" } }); } @Mock private HttpClient mockHttpClient; private HttpPost mockHttpPost; private HttpResponse mockHttpResponse; private HttpEntity mockHttpEntity; private InputStream mockInputStream; private InputStreamReader mockInputStreamReader; private BufferedReader mockBufferedReader; @Before public void setUp() { MockitoAnnotations.initMocks(this); xstream = new XStream(); } public SearchTest(String folder) { this.preFile= folder + "/inpRows.json"; this.expectedPreFile= folder + "/inpRowsExpected.json"; this.postFile= folder + "/outRows.json"; this.expectedpostFile= folder + "/outRowsExpected.json"; } @Test /** * Simulates the calling of a handler * * Setup @Parameters with a list of folders containing the test files. A test is performed for each entry * @throws CallbackHandlerException * @throws IOException * @throws JSONException * @throws SecurityException * @throws NoSuchFieldException * @throws IllegalArgumentException * @throws IllegalAccessException */ public void testHandler() throws Exception { /**********Set the expected results for the mocked methods****************/ Mockito.when(mockHttpClient.execute(mockHttpPost)).thenReturn(mockHttpResponse); Mockito.when(mockHttpResponse.getEntity()).thenReturn(mockHttpEntity); Mockito.when(mockHttpEntity.getContent()).thenReturn(mockInputStream); PowerMockito.whenNew(InputStreamReader.class).withArguments(mockInputStream).thenReturn(mockInputStreamReader); PowerMockito.whenNew(BufferedReader.class).withArguments(mockInputStreamReader).thenReturn(mockBufferedReader); PowerMockito.whenNew(JSONObject.class).withArguments(Matchers.any(String.class)).thenReturn(jsonStub); searchHandler.pre(); //Call the actual Pre method to be tested } } 

Any suggestions why I'm getting this error?

Thanks

2
  • You've cut off the stack trace too early - what's under the "Caused by ..." line? Commented Dec 18, 2013 at 12:02
  • Can You share the complete code here , I too have the same issue Commented Jan 19, 2019 at 7:22

1 Answer 1

3

Try to put initMocks in @Before instead of @BeforeClass method. What you are trying to do is to set some fields in a static method. That's my guess.

Sign up to request clarification or add additional context in comments.

8 Comments

I did as per your suggestion but all the MOCKS are null, thereby giving me NPE in my code. I've re-pasted my revised code above in my question.
Strangely, I'm only having the first mock object (mockHttpClient) as populated and rest everyone is null when inside the testHandler(). CAn you suggest why this is happening?
only mockHttpClient is annotated with @Mock, that's why.
add @Mock annotation to each mock field in your class
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.