@UnstableApi public class ExtendedMockito extends org.mockito.Mockito
E.g.
private class C { static int staticMethod(String arg) { return 23; } } @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
.
Constructor and Description |
---|
ExtendedMockito() |
Modifier and Type | Method and Description |
---|---|
static StaticCapableStubber |
doAnswer(org.mockito.stubbing.Answer answer)
Same as
Mockito.doAnswer(Answer) but adds the ability to stub static method calls via
StaticCapableStubber.when(MockedMethod) and
StaticCapableStubber.when(MockedVoidMethod) . |
static StaticCapableStubber |
doCallRealMethod()
Same as
Mockito.doCallRealMethod() but adds the ability to stub static method calls
via StaticCapableStubber.when(MockedMethod) and
StaticCapableStubber.when(MockedVoidMethod) . |
static StaticCapableStubber |
doNothing()
Same as
Mockito.doNothing() but adds the ability to stub static method calls via
StaticCapableStubber.when(MockedMethod) and
StaticCapableStubber.when(MockedVoidMethod) . |
static StaticCapableStubber |
doReturn(java.lang.Object toBeReturned)
Same as
Mockito.doReturn(Object) but adds the ability to stub static method calls
via StaticCapableStubber.when(MockedMethod) and
StaticCapableStubber.when(MockedVoidMethod) . |
static StaticCapableStubber |
doReturn(java.lang.Object toBeReturned,
java.lang.Object... toBeReturnedNext)
Same as
Mockito.doReturn(Object, Object...) but adds the ability to stub static
method calls via StaticCapableStubber.when(MockedMethod) and
StaticCapableStubber.when(MockedVoidMethod) . |
static StaticCapableStubber |
doThrow(java.lang.Class<? extends java.lang.Throwable> toBeThrown)
Same as
Mockito.doThrow(Class) but adds the ability to stub static method calls via
StaticCapableStubber.when(MockedMethod) and
StaticCapableStubber.when(MockedVoidMethod) . |
static StaticCapableStubber |
doThrow(java.lang.Class<? extends java.lang.Throwable> toBeThrown,
java.lang.Class<? extends java.lang.Throwable>... toBeThrownNext)
Same as
Mockito.doThrow(Class, Class...) but adds the ability to stub static method
calls via StaticCapableStubber.when(MockedMethod) and
StaticCapableStubber.when(MockedVoidMethod) . |
static StaticCapableStubber |
doThrow(java.lang.Throwable... toBeThrown)
Same as
Mockito.doThrow(Throwable...) but adds the ability to stub static method
calls via StaticCapableStubber.when(MockedMethod) and
StaticCapableStubber.when(MockedVoidMethod) . |
static StaticInOrder |
inOrder(java.lang.Object... mocksAndMarkers)
Same as
Mockito.inOrder(Object...) but adds the ability to verify static method
calls via StaticInOrder.verify(MockedMethod) ,
StaticInOrder.verify(MockedVoidMethod) ,
StaticInOrder.verify(MockedMethod, VerificationMode) , and
StaticInOrder.verify(MockedVoidMethod, VerificationMode) . |
static StaticMockitoSessionBuilder |
mockitoSession()
Same as
Mockito.mockitoSession() but adds the ability to mock static methods
calls via StaticMockitoSessionBuilder.mockStatic(Class) ,
StaticMockitoSessionBuilder.mockStatic(Class, Answer) , and StaticMockitoSessionBuilder.mockStatic(Class, MockSettings) ; |
static void |
spyOn(java.lang.Object toMock)
Make an existing object a spy.
|
static java.lang.Object[] |
staticMockMarker(java.lang.Class<?>... clazz)
Same as
staticMockMarker(Class) but for multiple classes at once. |
static <T> T |
staticMockMarker(java.lang.Class<T> clazz)
Many methods of mockito take mock objects.
|
static void |
verify(MockedMethod method)
To be used for static mocks/spies in place of
Mockito.verify(Object) . |
static void |
verify(MockedMethod method,
org.mockito.verification.VerificationMode mode)
To be used for static mocks/spies in place of
Mockito.verify(Object, VerificationMode) . |
static void |
verify(MockedVoidMethod method)
To be used for static mocks/spies in place of
Mockito.verify(Object) when calling
void methods. |
static void |
verify(MockedVoidMethod method,
org.mockito.verification.VerificationMode mode)
To be used for static mocks/spies in place of
Mockito.verify(Object, VerificationMode) when calling void methods. |
after, atLeast, atLeastOnce, atMost, calls, clearInvocations, description, framework, ignoreStubs, mock, mock, mock, mock, mockingDetails, never, only, reset, spy, spy, timeout, times, validateMockitoUsage, verifyNoMoreInteractions, verifyZeroInteractions, when, withSettings
any, 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, isNotNull, isNull, isNull, longThat, matches, matches, notNull, notNull, nullable, refEq, same, shortThat, startsWith
public static StaticCapableStubber doAnswer(org.mockito.stubbing.Answer answer)
Mockito.doAnswer(Answer)
but adds the ability to stub static method calls via
StaticCapableStubber.when(MockedMethod)
and
StaticCapableStubber.when(MockedVoidMethod)
.public static StaticCapableStubber doCallRealMethod()
Mockito.doCallRealMethod()
but adds the ability to stub static method calls
via StaticCapableStubber.when(MockedMethod)
and
StaticCapableStubber.when(MockedVoidMethod)
.public static StaticCapableStubber doNothing()
Mockito.doNothing()
but adds the ability to stub static method calls via
StaticCapableStubber.when(MockedMethod)
and
StaticCapableStubber.when(MockedVoidMethod)
.public static StaticCapableStubber doReturn(java.lang.Object toBeReturned)
Mockito.doReturn(Object)
but adds the ability to stub static method calls
via StaticCapableStubber.when(MockedMethod)
and
StaticCapableStubber.when(MockedVoidMethod)
.public static StaticCapableStubber doReturn(java.lang.Object toBeReturned, java.lang.Object... toBeReturnedNext)
Mockito.doReturn(Object, Object...)
but adds the ability to stub static
method calls via StaticCapableStubber.when(MockedMethod)
and
StaticCapableStubber.when(MockedVoidMethod)
.public static StaticCapableStubber doThrow(java.lang.Class<? extends java.lang.Throwable> toBeThrown)
Mockito.doThrow(Class)
but adds the ability to stub static method calls via
StaticCapableStubber.when(MockedMethod)
and
StaticCapableStubber.when(MockedVoidMethod)
.@SafeVarargs public static StaticCapableStubber doThrow(java.lang.Class<? extends java.lang.Throwable> toBeThrown, java.lang.Class<? extends java.lang.Throwable>... toBeThrownNext)
Mockito.doThrow(Class, Class...)
but adds the ability to stub static method
calls via StaticCapableStubber.when(MockedMethod)
and
StaticCapableStubber.when(MockedVoidMethod)
.public static StaticCapableStubber doThrow(java.lang.Throwable... toBeThrown)
Mockito.doThrow(Throwable...)
but adds the ability to stub static method
calls via StaticCapableStubber.when(MockedMethod)
and
StaticCapableStubber.when(MockedVoidMethod)
.@UnstableApi public static <T> T staticMockMarker(java.lang.Class<T> clazz)
clazz
- The class object the marker should be crated forinOrder(Object...)
,
Mockito.clearInvocations(Object...)
,
Mockito.ignoreStubs(Object...)
,
Mockito.mockingDetails(Object)
,
Mockito.reset(Object[])
,
Mockito.verifyNoMoreInteractions(Object...)
,
Mockito.verifyZeroInteractions(Object...)
@UnstableApi public static java.lang.Object[] staticMockMarker(java.lang.Class<?>... clazz)
staticMockMarker(Class)
but for multiple classes at once.@UnstableApi public static void spyOn(java.lang.Object toMock)
This does not clone the existing objects. If a method is stubbed on a spy converted by this method all references to the already existing object will be affected by the stubbing.
toMock
- The existing object to convert into a spypublic static void verify(MockedVoidMethod method)
Mockito.verify(Object)
when calling
void methods.
E.g.
private class C { void instanceMethod(String arg) {} static void staticMethod(String arg) {} } @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(); }
@UnstableApi public static void verify(MockedMethod method)
Mockito.verify(Object)
.
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; } } @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(); }
@UnstableApi public static void verify(MockedVoidMethod method, org.mockito.verification.VerificationMode mode)
Mockito.verify(Object, VerificationMode)
when calling void methods.verify(MockedVoidMethod)
@UnstableApi public static void verify(MockedMethod method, org.mockito.verification.VerificationMode mode)
Mockito.verify(Object, VerificationMode)
.verify(MockedMethod)
@UnstableApi public static StaticInOrder inOrder(java.lang.Object... mocksAndMarkers)
Mockito.inOrder(Object...)
but adds the ability to verify static method
calls via StaticInOrder.verify(MockedMethod)
,
StaticInOrder.verify(MockedVoidMethod)
,
StaticInOrder.verify(MockedMethod, VerificationMode)
, and
StaticInOrder.verify(MockedVoidMethod, VerificationMode)
.
To verify static method calls, the result of staticMockMarker(Class)
has to be
passed to the mocksAndMarkers
parameter. It is possible to mix static and instance
mocking.
public static StaticMockitoSessionBuilder mockitoSession()
Mockito.mockitoSession()
but adds the ability to mock static methods
calls via StaticMockitoSessionBuilder.mockStatic(Class)
,
StaticMockitoSessionBuilder.mockStatic(Class, Answer)
, and StaticMockitoSessionBuilder.mockStatic(Class, MockSettings)
;
All mocking spying will be removed once the session is finished.