Class ThrowsException

  • All Implemented Interfaces:
    java.io.Serializable, Answer<java.lang.Object>, ValidableAnswer

    public class ThrowsException
    extends java.lang.Object
    implements Answer<java.lang.Object>, ValidableAnswer, java.io.Serializable
    An answer that always throws the same throwable.
    See Also:
    Serialized Form
    • Constructor Detail

      • ThrowsException

        public ThrowsException​(java.lang.Throwable throwable)
        Creates a new answer always throwing the given throwable. If it is null, answer validation will fail.
    • Method Detail

      • answer

        public java.lang.Object answer​(InvocationOnMock invocation)
                                throws java.lang.Throwable
        Specified by:
        answer in interface Answer<java.lang.Object>
        Parameters:
        invocation - the invocation on the mock.
        Returns:
        the value to be returned
        Throws:
        java.lang.Throwable - the throwable to be thrown
      • validateFor

        public void validateFor​(InvocationOnMock invocation)
        Description copied from interface: ValidableAnswer
        Validation of the answer at stub time for the given invocation.

        This method will be called by Mockito.

        The implementation must throw an MockitoException to indicate that this answer is not valid for the given invocation. If the validation succeed the implementation must simply return without throwing.

        Specified by:
        validateFor in interface ValidableAnswer
        Parameters:
        invocation - The stubbed invocation