Class MockitoTools

java.lang.Object
no.mnemonic.commons.testtools.MockitoTools

public class MockitoTools extends Object
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    match(Predicate<T> predicate)
    Simplify testing with mockito and argThat by doing
    static <T, U extends T>
    T
    match(Predicate<T> predicate, Class<U> expectedClass)
    Same as match(Predicate), but with a specific expected argument class, to use if the verified method takes a superclass of expected argument verify(mock).method(match(c->c.getValue()==expectedValue, ExpectedClass.class))

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MockitoTools

      public MockitoTools()
  • Method Details

    • match

      public static <T> T match(Predicate<T> predicate)
      Simplify testing with mockito and argThat by doing

      verify(mock).method(match(arg->arg.isSomething()));

      Type Parameters:
      T - the argument type
      Parameters:
      predicate - predicate to match the expected argument
      Returns:
      a mock reporter which matches the argument using this predicate
    • match

      public static <T, U extends T> T match(Predicate<T> predicate, Class<U> expectedClass)
      Same as match(Predicate), but with a specific expected argument class, to use if the verified method takes a superclass of expected argument verify(mock).method(match(c->c.getValue()==expectedValue, ExpectedClass.class))
      Type Parameters:
      T - mocked method argument type
      U - expected argument type, subclass of T
      Parameters:
      expectedClass - Expected argument class
      predicate - predicate to test on the argument
      Returns:
      true if argument is instanceof U and passes the predicate test