@UnstableApi public class StaticCapableStubber extends java.lang.Object implements org.mockito.stubbing.Stubber
Stubber but supports settings up stubbing of static methods via
when(MockedMethod) and when(MockedVoidMethod).| Modifier and Type | Method and Description |
|---|---|
StaticCapableStubber |
doAnswer(org.mockito.stubbing.Answer answer) |
StaticCapableStubber |
doCallRealMethod() |
StaticCapableStubber |
doNothing() |
StaticCapableStubber |
doReturn(java.lang.Object toBeReturned) |
StaticCapableStubber |
doReturn(java.lang.Object toBeReturned,
java.lang.Object... nextToBeReturned) |
StaticCapableStubber |
doThrow(java.lang.Class<? extends java.lang.Throwable> toBeThrown) |
StaticCapableStubber |
doThrow(java.lang.Class<? extends java.lang.Throwable> toBeThrown,
java.lang.Class<? extends java.lang.Throwable>... nextToBeThrown) |
StaticCapableStubber |
doThrow(java.lang.Throwable... toBeThrown) |
<T> void |
when(MockedMethod<T> method)
Set up stubbing for a static method.
|
void |
when(MockedVoidMethod method)
Set up stubbing for a static void method.
|
<T> T |
when(T mock) |
public <T> T when(T mock)
when in interface org.mockito.stubbing.Stubber@UnstableApi public void when(MockedVoidMethod method)
private class C {
void instanceMethod(String arg) {}
static void staticMethod(String arg) {}
}
@Test
public void test() {
// instance mocking
C mock = mock(C.class);
doThrow(Exception.class).when(mock).instanceMethod(eq("Hello));
assertThrows(Exception.class, mock.instanceMethod("Hello"));
// static mocking
MockitoSession session = mockitoSession().staticMock(C.class).startMocking();
doThrow(Exception.class).when(() -> C.instanceMethod(eq("Hello));
assertThrows(Exception.class, C.staticMethod("Hello"));
session.finishMocking();
}
method - The method to stub as a lambda. This should only call a single stubbable
static method.@UnstableApi public <T> void when(MockedMethod<T> method)
private class C {
int instanceMethod(String arg) {
return 1;
}
int static staticMethod(String arg) {
return 1;
}
}
@Test
public void test() {
// instance mocking
C mock = mock(C.class);
doReturn(2).when(mock).instanceMethod(eq("Hello));
assertEquals(2, mock.instanceMethod("Hello"));
// static mocking
MockitoSession session = mockitoSession().staticMock(C.class).startMocking();
doReturn(2).when(() -> C.instanceMethod(eq("Hello));
assertEquals(2, C.staticMethod("Hello"));
session.finishMocking();
}
T - Return type of the stubbed methodmethod - The method to stub as a lambda. This should only call a single stubbable
static method.public StaticCapableStubber doThrow(java.lang.Throwable... toBeThrown)
doThrow in interface org.mockito.stubbing.Stubberpublic StaticCapableStubber doThrow(java.lang.Class<? extends java.lang.Throwable> toBeThrown)
doThrow in interface org.mockito.stubbing.Stubber@SafeVarargs public final StaticCapableStubber doThrow(java.lang.Class<? extends java.lang.Throwable> toBeThrown, java.lang.Class<? extends java.lang.Throwable>... nextToBeThrown)
doThrow in interface org.mockito.stubbing.Stubberpublic StaticCapableStubber doAnswer(org.mockito.stubbing.Answer answer)
doAnswer in interface org.mockito.stubbing.Stubberpublic StaticCapableStubber doNothing()
doNothing in interface org.mockito.stubbing.Stubberpublic StaticCapableStubber doReturn(java.lang.Object toBeReturned)
doReturn in interface org.mockito.stubbing.Stubberpublic StaticCapableStubber doReturn(java.lang.Object toBeReturned, java.lang.Object... nextToBeReturned)
doReturn in interface org.mockito.stubbing.Stubberpublic StaticCapableStubber doCallRealMethod()
doCallRealMethod in interface org.mockito.stubbing.Stubber