Class CallsRealMethods

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

    public class CallsRealMethods
    extends java.lang.Object
    implements Answer<java.lang.Object>, ValidableAnswer, java.io.Serializable
    Optional Answer that adds partial mocking support

    Answer can be used to define the return values of unstubbed invocations.

    This implementation can be helpful when working with legacy code. When this implementation is used, unstubbed methods will delegate to the real implementation. This is a way to create a partial mock object that calls real methods by default.

    As usual you are going to read the partial mock warning: Object oriented programming is more less tackling complexity by dividing the complexity into separate, specific, SRPy objects. How does partial mock fit into this paradigm? Well, it just doesn't... Partial mock usually means that the complexity has been moved to a different method on the same object. In most cases, this is not the way you want to design your application.

    However, there are rare cases when partial mocks come handy: dealing with code you cannot change easily (3rd party interfaces, interim refactoring of legacy code etc.) However, I wouldn't use partial mocks for new, test-driven & well-designed code.

    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static long serialVersionUID  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object answer​(InvocationOnMock invocation)  
      void validateFor​(InvocationOnMock invocation)
      Validation of the answer at stub time for the given invocation.
      • Methods inherited from class java.lang.Object

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

      • CallsRealMethods

        public CallsRealMethods()
    • 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