Class Strict<T>

java.lang.Object
com.atlassian.jira.mock.Strict<T>
All Implemented Interfaces:
org.mockito.stubbing.Answer<T>

public class Strict<T> extends Object implements org.mockito.stubbing.Answer<T>
Mockito mocks are lenient by default, and this makes them throw an exception for anything that was not expressly stubbed. Note that this means the when(mock.method()) style of stubbing will not work because the mock.method() call will throw an exception instead of returning null. You will need to use the doReturn(null).when(mock).method() style of stubbing, instead.

Why would you do this instead of just letting it throw a NullPointerException when the unstubbed method is invoked? Because this will actually tell you what the unexpected invocation was. Much more helpful!

Since:
v6.2
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    answer(org.mockito.invocation.InvocationOnMock invocationOnMock)
    Throws an exception that reports the unexpected method invocation.
    static <T> T
    reject(T mockOrSpy)
    Factory method for stubbing an individual method as rejected.
    static <T> Strict<T>
    Factory method that will infer the correct type from the context
    static <T> Strict<T>
    strict(Class<T> tClass)
    Factory method that will infer the correct type from the supplied class

    Methods inherited from class java.lang.Object

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

    • Strict

      public Strict()
  • Method Details

    • strict

      public static <T> Strict<T> strict()
      Factory method that will infer the correct type from the context
    • strict

      public static <T> Strict<T> strict(Class<T> tClass)
      Factory method that will infer the correct type from the supplied class
    • reject

      public static <T> T reject(T mockOrSpy)
      Factory method for stubbing an individual method as rejected.

      Example:

           Strict.reject(mock).methodThatShouldNotGetCalled();
       
      >
    • answer

      public T answer(org.mockito.invocation.InvocationOnMock invocationOnMock) throws Throwable
      Throws an exception that reports the unexpected method invocation.
      Specified by:
      answer in interface org.mockito.stubbing.Answer<T>
      Parameters:
      invocationOnMock - the invocation that reached us
      Returns:
      never returns normally
      Throws:
      AssertionError - reporting the invocation
      Throwable