-
@UnstableApi() public class ExtendedMockito extends Mockito
Mockito extended with the ability to stub static methods.
E.g.
private class C { static int staticMethod(String arg) { return 23; } }{@literal @}Test public void test() { // static mocking MockitoSession session = mockitoSession().staticSpy(C.class).startMocking(); try { doReturn(42).when(() -> {return C.staticMethod(eq("Arg"));}); assertEquals(42, C.staticMethod("Arg")); verify(() -> C.staticMethod(eq("Arg")); } finally { session.finishMocking(); } }It is possible to use this class for instance mocking too. Hence you can use it as a full replacement for Mockito.
This is a prototype that is intended to eventually be upstreamed into mockito proper. Some APIs might change. All such APIs are annotated with UnstableApi.
-
-
Method Summary
Modifier and Type Method Description static StaticCapableStubberdoAnswer(Answer answer)Same as doAnswer but adds the ability to stub static method calls via when and when. static StaticCapableStubberdoCallRealMethod()Same as doCallRealMethod but adds the ability to stub static method callsvia when and when. static StaticCapableStubberdoNothing()Same as doNothing but adds the ability to stub static method calls via when and when. static StaticCapableStubberdoReturn(Object toBeReturned)Same as doReturn but adds the ability to stub static method callsvia when and when. static StaticCapableStubberdoReturn(Object toBeReturned, Array<Object> toBeReturnedNext)Same as doReturn but adds the ability to stub staticmethod calls via when and when. static StaticCapableStubberdoThrow(Class<out Throwable> toBeThrown)Same as doThrow but adds the ability to stub static method calls via when and when. static StaticCapableStubberdoThrow(Class<out Throwable> toBeThrown, Array<Class<out Throwable>> toBeThrownNext)Same as doThrow but adds the ability to stub static methodcalls via when and when. static StaticCapableStubberdoThrow(Array<Throwable> toBeThrown)Same as doThrow but adds the ability to stub static methodcalls via when and when. static <T> TstaticMockMarker(Class<T> clazz)Many methods of mockito take mock objects. static Array<Object>staticMockMarker(Array<Class<out Object>> clazz)Same as staticMockMarker but for multiple classes at once. static voidspyOn(Object toSpy)Make an existing object a spy. static voidverify(MockedVoidMethod method)To be used for static mocks/spies in place of verify when callingvoid methods. static voidverify(MockedMethod method)To be used for static mocks/spies in place of verify. static voidverify(MockedVoidMethod method, VerificationMode mode)To be used for static mocks/spies in place of verify when calling void methods. static voidverify(MockedMethod method, VerificationMode mode)To be used for static mocks/spies in place of verify. static StaticInOrderinOrder(Array<Object> mocksAndMarkers)Same as inOrder but adds the ability to verify static methodcalls via verify, verify, verify, and verify. static StaticMockitoSessionBuildermockitoSession()Same as mockitoSession but adds the ability to mock static methodscalls via mockStatic, mockStatic, and ;All mocking spying will be removed once the session is finished. -
Methods inherited from class org.mockito.Mockito
after, atLeast, atLeastOnce, atMost, atMostOnce, calls, clearInvocations, description, doAnswer, doCallRealMethod, doNothing, doReturn, doThrow, framework, ignoreStubs, inOrder, lenient, mock, mockingDetails, mockitoSession, never, only, reset, spy, timeout, times, validateMockitoUsage, verify, verifyNoMoreInteractions, verifyZeroInteractions, when, withSettings -
Methods inherited from class org.mockito.ArgumentMatchers
any, anyBoolean, anyByte, anyChar, anyCollection, anyCollectionOf, anyDouble, anyFloat, anyInt, anyIterable, anyIterableOf, anyList, anyListOf, anyLong, anyMap, anyMapOf, anyObject, anySet, anySetOf, anyShort, anyString, anyVararg, argThat, booleanThat, byteThat, charThat, contains, doubleThat, endsWith, eq, eq, eq, eq, eq, eq, eq, eq, eq, floatThat, intThat, isA, isNotNull, isNull, longThat, matches, notNull, nullable, refEq, same, shortThat, startsWith -
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Method Detail
-
doAnswer
static StaticCapableStubber doAnswer(Answer answer)
-
doCallRealMethod
static StaticCapableStubber doCallRealMethod()
-
doNothing
static StaticCapableStubber doNothing()
-
doReturn
static StaticCapableStubber doReturn(Object toBeReturned)
-
doReturn
static StaticCapableStubber doReturn(Object toBeReturned, Array<Object> toBeReturnedNext)
-
doThrow
static StaticCapableStubber doThrow(Class<out Throwable> toBeThrown)
-
doThrow
@SafeVarargs() static StaticCapableStubber doThrow(Class<out Throwable> toBeThrown, Array<Class<out Throwable>> toBeThrownNext)
-
doThrow
static StaticCapableStubber doThrow(Array<Throwable> toBeThrown)
-
staticMockMarker
@UnstableApi() static <T> T staticMockMarker(Class<T> clazz)
Many methods of mockito take mock objects. To be able to call the same methods for staticmocking, this method gets a marker object that can be used instead.
- Parameters:
clazz- The class object the marker should be crated for
-
staticMockMarker
@UnstableApi() static Array<Object> staticMockMarker(Array<Class<out Object>> clazz)
Same as staticMockMarker but for multiple classes at once.
-
spyOn
@UnstableApi() static void spyOn(Object toSpy)
Make an existing object a spy.
This does not clone the existing objects. If a method is stubbed on a spyconverted by this method all references to the already existing object will be affected bythe stubbing.
- Parameters:
toSpy- The existing object to convert into a spy
-
verify
static void verify(MockedVoidMethod method)
To be used for static mocks/spies in place of verify when callingvoid methods.
E.g.
private class C { void instanceMethod(String arg) {} static void staticMethod(String arg) {} }{@literal @}Test public void test() { // instance mocking C mock = mock(C.class); mock.instanceMethod("Hello"); verify(mock).mockedVoidInstanceMethod(eq("Hello")); // static mocking MockitoSession session = mockitoSession().staticMock(C.class).startMocking(); C.staticMethod("World"); verify(() -> C.staticMethod(eq("World")); session.finishMocking(); }
-
verify
@UnstableApi() static void verify(MockedMethod method)
To be used for static mocks/spies in place of verify.
E.g. (please notice the 'return' in the lambda when verifying the static call)
private class C { int instanceMethod(String arg) { return 1; } int static staticMethod(String arg) { return 2; } }{@literal @}Test public void test() { // instance mocking C mock = mock(C.class); mock.instanceMethod("Hello"); verify(mock).mockedVoidInstanceMethod(eq("Hello")); // static mocking MockitoSession session = mockitoSession().staticMock(C.class).startMocking(); C.staticMethod("World"); verify(() -> {return C.staticMethod(eq("World");}); session.finishMocking(); }
-
verify
@UnstableApi() static void verify(MockedVoidMethod method, VerificationMode mode)
To be used for static mocks/spies in place of verify when calling void methods.
-
verify
@UnstableApi() static void verify(MockedMethod method, VerificationMode mode)
To be used for static mocks/spies in place of verify.
-
inOrder
@UnstableApi() static StaticInOrder inOrder(Array<Object> mocksAndMarkers)
Same as inOrder but adds the ability to verify static methodcalls via verify, verify, verify, and verify.
To verify static method calls, the result of staticMockMarker has to bepassed to the
{@code mocksAndMarkers}parameter. It is possible to mix static and instancemocking.
-
mockitoSession
static StaticMockitoSessionBuilder mockitoSession()
Same as mockitoSession but adds the ability to mock static methodscalls via mockStatic, mockStatic, and ;
All mocking spying will be removed once the session is finished.
-
-
-
-