[Solved]org.mockito.exceptions.base.MockitoException

MockitoException  is RuntimeException raised by Mockito framework to emit an error either due to Mockito, or due to the User. Mockito throws exception in form of stacktrace with suggested solutions.

StackTrace for Application calls:

StackTraceElement[] Throwable.getStackTrace()

StackTrace with Mockito and API calls:

StackTraceElement[] getUnifilteredStackTrace()

Constructors

  • MockitoException(String message) : Will throw exception with message.
  • MockitoException(String message, Throwable T) : Will throw exception with message and stacktrace.

Mockito Exception Subclasses

MockitoException Hierarchy
MockitoException Hierarchy

Example

In below example , using OngoingStubbing on mock object of Map interface where setting stub object for get() method which accept any input String and return response as String as set in thenReturn() method.

import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.Map;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.stubbing.OngoingStubbing;

public class MockitoApplicationTester {

@Test
public void second_stubbing_throws_InCorrectUseOfAPI() throws Exception {
// Mock of Map interface
Map<String, String> map = mock(Map.class);

OngoingStubbing<String> mapOngoingStubbing = when(map.get(anyString()));
try {
mapOngoingStubbing.thenReturn("1st stubbing");
//This method will throw exception
mapOngoingStubbing.thenReturn("2nd stubbing");
Assert.assertEquals(map.get("Test"), "1st stubbing");
} catch (MockitoException e) {
StackTraceElement[] stacks = e.getStackTrace();
for (StackTraceElement stack : stacks) {
System.out.println(stack.getMethodName());
}

StackTraceElement[] ustacks = e.getUnfilteredStackTrace();
for (StackTraceElement stack : ustacks) {
System.out.println(stack.getMethodName());
}
throw e;

}
}

Output

org.mockito.exceptions.base.MockitoException:
Incorrect use of API detected here:
-> at com.facingissuesonit.mockito.MockitoTestExamples.MockitoApplicationTester.second_stubbing_throws_IndexOutOfBoundsException(MockitoApplicationTester.java:48)

You probably stored a reference to OngoingStubbing returned by when() and called stubbing methods like thenReturn() on this reference more than once.
Examples of correct usage:
when(mock.isOk()).thenReturn(true).thenReturn(false).thenThrow(exception);
when(mock.isOk()).thenReturn(true, false).thenThrow(exception);

at com.facingissuesonit.mockito.MockitoTestExamples.MockitoApplicationTester.second_stubbing_throws_IndexOutOfBoundsException(MockitoApplicationTester.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)<span 				data-mce-type="bookmark" 				id="mce_SELREST_start" 				data-mce-style="overflow:hidden;line-height:0" 				style="overflow:hidden;line-height:0" 			></span>

Solutions

Above example is throwing MockitoException with message as “Incorrect use of API detected here”  because of thenReturn() method set two times value for single mock stub object. For solving above issue comment one of thenRetun() statement from try block.

References

https://static.javadoc.io/org.mockito/mockito-core/2.6.5/org/mockito/exceptions/base/MockitoException.html#getUnfilteredStackTrace()

Know More

To know more about Junit, Mockito and exception solutions follow below links: