public class MoreAnswers extends Object
This class is meant to be analogous to Answers, and has a different name solely to make
it easier to use them together in the same test without having to fully qualify them.
| Modifier and Type | Method and Description |
|---|---|
static <T> FlipFlop<T> |
flipFlop(org.mockito.stubbing.Answer<?> answer1)
A factory for a flip-flop answer that initially uses the
RETURNS_DEFAULTS setting, but switches to the
provided answer1 when flipped. |
static <T> FlipFlop<T> |
flipFlop(org.mockito.stubbing.Answer<?> answer0,
org.mockito.stubbing.Answer<?> answer1)
A factory for flip-flop answers.
|
static <T> T |
reject(T mockOrSpy)
Factory method for explicitly stubbing an individual method as having a
strict answer. |
static <T> Strict<T> |
strict()
Factory method for a
strict Answer<T> that will infer the
correct type from the context. |
static <T> Strict<T> |
strict(Class<T> tClass)
Factory method for a
strict Answer<T> that will infer the
correct type from the supplied class. |
static <T> T |
strictMock(Class<T> tClass)
Factory method for creating a strict mock directly.
|
public static <T> FlipFlop<T> flipFlop(org.mockito.stubbing.Answer<?> answer1)
RETURNS_DEFAULTS setting, but switches to the
provided answer1 when flipped.
Example:
final FlipFlop<Object> flipFlop = flipFlop(strict());
final Foo mockFoo = mock(Foo.class, flipFlop);
// Mock using the normal when(...) style possible
when(mockFoo.call1()).thenReturn("mocked");
// switch from RETURNS_DEFAULTS to strict()
flipFlop.flip();
assertThat(mockFoo.call1(), is("mocked")); // ok, because call1() was already mocked out
mockFoo.call2(); // throws AssertionError
T - the inferred type of the answers to giveanswer1 - the answer that you get when the flip-flop is flipped. Its return
type is deliberately not checked to keep the type inference simple. If it returns an answer
that cannot be matched to the inferred type, then you are likely to get a
ClassCastException at runtime.public static <T> FlipFlop<T> flipFlop(org.mockito.stubbing.Answer<?> answer0, org.mockito.stubbing.Answer<?> answer1)
Example:
final Answer<String> yea = invocation -> "YEA!";
final Answer<String> nay = invocation -> "Nay.";
final FlipFlop<Object> flipFlop = flipFlop(yea, nay);
final Foo mockFoo = mock(Foo.class, flipFlop);
assertThat(mockFoo.get(), is("YEA!"));
flipFlop.flip();
assertThat(mockFoo.get(), is("Nay."));
flipFlop.flip();
assertThat(mockFoo.get(), is("YEA!"));
Hrmmm... Maybe this method should be called politician, instead? ;P
T - the inferred type of the answers to giveanswer0 - the answer that you initially get. The answer's type is deliberately not checked to keep the
type inference simple. If it returns an answer that cannot be matched to the inferred type,
then you are likely to get a ClassCastException at runtime.answer1 - the answer that you get when the flip-flop is flipped. As with
answer0, the answer's type has been left unchecked.public static <T> Strict<T> strict()
strict Answer<T> that will infer the
correct type from the context.
Example:
final Foo mockFoo = mock(Foo.class, strict());
mockFoo.methodThatShouldNotBeCalled(); // throws AssertionError
public static <T> Strict<T> strict(Class<T> tClass)
strict Answer<T> that will infer the
correct type from the supplied class.
Example:
final Foo mockFoo = mock(Foo.class);
final Answer<String> die = strict(String.class);
when(mockFoo.doNotGetString()).thenAnswer(die);
mockFoo.doNotGetString(); // throws AssertionError
tClass - the type from which to infer the answer's return typepublic static <T> T strictMock(Class<T> tClass)
Example:
final Foo mockFoo = strictMock(Foo.class);
doReturn("mocked").when(mockFoo).call1();
assertThat(mockFoo.call1(), is("mocked"));
mockFoo.call2(); // throws AssertionError
tClass - the type of mock to createpublic static <T> T reject(T mockOrSpy)
strict answer.
This is similar to using verify with never(), except that it is declared in advance
and the error is thrown from where the call is made rather than from where it is later verified.
Example:
final Foo mockFoo = mock(Foo.class);
reject(mockFoo).methodThatShouldNotGetCalled();
mockFoo.methodThatShouldNotGetCalled(); // throws AssertionError
Copyright © 2017 Atlassian. All rights reserved.