Package org.dmfs.jems2.mockito
Class Mock
java.lang.Object
org.dmfs.jems2.mockito.Mock
A container of static methods for creating mocks in a single expression.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA type that combines aStubberGeneratorandFunction -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Mock.StubGenerator<T>answering(org.mockito.stubbing.Answer<T> answer) static <T> Mock.StubGenerator<T>Returns aMock.StubGeneratorthat configures aStubberdo nothing.static <T> TReturn a mock of the given class.static <T> TReturn a named mock of the given class.static <T> Mock.StubGenerator<T>returning(T value, T... values) Returns aMock.StubGeneratorthat configures aStubberto return the given values in the given order.static <T> Mock.StubGenerator<T>static <T> Tupdate(T mock, org.dmfs.jems2.Procedure<? super T>... methods) Updates a given mock.static <T,V> org.dmfs.jems2.Procedure<T> with(org.dmfs.jems2.ThrowingFunction<? super T, ? extends V> methodCall, Mock.StubGenerator<? extends V> stubGenerator, Mock.StubGenerator<? extends V>... stubGenerators) Returns aProcedurewhich configures a mock by setting the expectation on a method call.static <T,V> org.dmfs.jems2.Procedure<T> withVoid(org.dmfs.jems2.FragileProcedure<? super T, ? extends Throwable> methodCall, Mock.StubGenerator<? extends V> stubGenerator, Mock.StubGenerator<? extends V>... stubGenerators) Returns aProcedurewhich configures a mock by setting the expectation on a void method call.
-
Constructor Details
-
Mock
public Mock()
-
-
Method Details
-
mock
@SafeVarargs public static <T> T mock(Class<T> clazz, org.dmfs.jems2.Procedure<? super T>... methods) Return a mock of the given class. This takes a couple ofProcedures to set up the mock in a single expression.Example
Foo fooMock = mock(Foo.class, with(Foo::bar, returning("foobar")), with(f->f.baz(123), returning("foobaz123")));Note, in contrast to
Mockito.mock(Class), mocks returned by this method fail with anAssertionErrorwhen a non-mocked method (other thanObject.hashCode(),Object.equals(Object)andObject.toString()) is called. -
mock
@SafeVarargs public static <T> T mock(String name, Class<T> clazz, org.dmfs.jems2.Procedure<? super T>... methods) Return a named mock of the given class. The name is returned by theObject.toString()method of the resulting class. This takes a couple ofProcedures to set up the mock in a single expression.Example
Foo fooMock = mock("My Foo", Foo.class, with(Foo::bar, returning("foobar")), with(f->f.baz(123), returning("foobaz123")));Note, in contrast to
Mockito.mock(Class), mocks returned by this method fail with anAssertionErrorwhen a non-mocked method (other thanObject.hashCode(),Object.equals(Object)andObject.toString()) is called.- See Also:
-
update
Updates a given mock. This takes a couple ofProcedures to update the mock in a single expression.Example
update(foomock, with(Foo::bar, returning("foobar")), with(f->f.baz(123), returning("foobaz123"))); -
with
@SafeVarargs public static <T,V> org.dmfs.jems2.Procedure<T> with(org.dmfs.jems2.ThrowingFunction<? super T, ? extends V> methodCall, Mock.StubGenerator<? extends V> stubGenerator, Mock.StubGenerator<? extends V>... stubGenerators) Returns aProcedurewhich configures a mock by setting the expectation on a method call. The result of this is supposed to be passed tomock(Class, Procedure[]). -
withVoid
@SafeVarargs public static <T,V> org.dmfs.jems2.Procedure<T> withVoid(org.dmfs.jems2.FragileProcedure<? super T, ? extends Throwable> methodCall, Mock.StubGenerator<? extends V> stubGenerator, Mock.StubGenerator<? extends V>... stubGenerators) Returns aProcedurewhich configures a mock by setting the expectation on a void method call. The result of this is supposed to be passed tomock(Class, Procedure[]). -
returning
Returns aMock.StubGeneratorthat configures aStubberto return the given values in the given order.- See Also:
-
BaseStubber.doReturn(Object, Object...)
-
throwing
- See Also:
-
BaseStubber.doThrow(Throwable...)
-
answering
- See Also:
-
BaseStubber.doAnswer(Answer)
-
doingNothing
Returns aMock.StubGeneratorthat configures aStubberdo nothing.- See Also:
-
BaseStubber.doNothing()
-