Class Mock

java.lang.Object
org.dmfs.jems2.mockito.Mock

public final class Mock extends Object
A container of static methods for creating mocks in a single expression.
  • 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 of Procedures 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 an AssertionError when a non-mocked method (other than Object.hashCode(), Object.equals(Object) and Object.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 the Object.toString() method of the resulting class. This takes a couple of Procedures 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 an AssertionError when a non-mocked method (other than Object.hashCode(), Object.equals(Object) and Object.toString()) is called.

      See Also:
    • update

      @SafeVarargs public static <T> T update(T mock, org.dmfs.jems2.Procedure<? super T>... methods)
      Updates a given mock. This takes a couple of Procedures 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 a Procedure which configures a mock by setting the expectation on a method call. The result of this is supposed to be passed to mock(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 a Procedure which configures a mock by setting the expectation on a void method call. The result of this is supposed to be passed to mock(Class, Procedure[]).
    • returning

      @SafeVarargs public static <T> Mock.StubGenerator<T> returning(T value, T... values)
      Returns a Mock.StubGenerator that configures a Stubber to return the given values in the given order.
      See Also:
      • BaseStubber.doReturn(Object, Object...)
    • throwing

      public static <T> Mock.StubGenerator<T> throwing(Throwable... exception)
      Returns a Mock.StubGenerator that configures a Stubber to throw the given Throwables.
      See Also:
      • BaseStubber.doThrow(Throwable...)
    • answering

      public static <T> Mock.StubGenerator<T> answering(org.mockito.stubbing.Answer<T> answer)
      Returns a Mock.StubGenerator that configures a Stubber to answer with the given Answer.
      See Also:
      • BaseStubber.doAnswer(Answer)
    • doingNothing

      public static <T> Mock.StubGenerator<T> doingNothing()
      Returns a Mock.StubGenerator that configures a Stubber do nothing.
      See Also:
      • BaseStubber.doNothing()