Class MockTestSupport

  • All Implemented Interfaces:
    TestSupport

    public class MockTestSupport
    extends java.lang.Object
    implements TestSupport
    Mock test support.
    Author:
    Daniel Sagenschneider
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected static interface  MockTestSupport.TestLogic<R,​T extends java.lang.Throwable>
      Test logic interface.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <M> M createMock​(java.lang.Class<M> classToMock)
      Creates a mock object registering the mock object with registry for management.
      <M> M createSynchronizedMock​(java.lang.Class<M> interfaceToMock)
      Creates a thread safe mock object.
      protected <R,​T extends java.lang.Throwable>
      R
      doTest​(MockTestSupport.TestLogic<R,​T> test)
      Undertakes test wrapping with mock object replay and verify.
      void init​(org.junit.jupiter.api.extension.ExtensionContext context)
      Intialise.
      <T> T param​(T value)
      Wraps a parameter value when attempting to capture.
      <T> T paramType​(java.lang.Class<T> type)
      Wraps a parameter type expected.
      <T> void recordReturn​(java.lang.Object mockObject, T ignore, T recordedReturn)
      Convenience method to record a method and its return on a mock object.
      <T> void recordReturn​(java.lang.Object mockObject, T ignore, T recordedReturn, ArgumentsMatcher matcher)
      Convenience method to record a method, an ArgumentsMatcher and return value.
      <T> void recordThrows​(java.lang.Object mockObject, T ignore, java.lang.Throwable exception)
      Convenience method to record an Exception.
      void recordVoid​(java.lang.Object mockObject, ArgumentsMatcher matcher)
      Convenience method to record void method.
      void replayMockObjects()
      Flags all the mock objects to replay.
      void verifyMockObjects()
      Verifies all mock objects.
      • Methods inherited from class java.lang.Object

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

      • MockTestSupport

        public MockTestSupport()
        Default instantiation.
    • Method Detail

      • init

        public void init​(org.junit.jupiter.api.extension.ExtensionContext context)
                  throws java.lang.Exception
        Description copied from interface: TestSupport
        Intialise.
        Specified by:
        init in interface TestSupport
        Parameters:
        context - ExtensionContext.
        Throws:
        java.lang.Exception - If fails to init.
      • createMock

        public <M> M createMock​(java.lang.Class<M> classToMock)
        Creates a mock object registering the mock object with registry for management.
        Type Parameters:
        M - Interface type.
        Parameters:
        classToMock - Class to be mocked.
        Returns:
        Mock object.
      • createSynchronizedMock

        public <M> M createSynchronizedMock​(java.lang.Class<M> interfaceToMock)
        Creates a thread safe mock object.
        Type Parameters:
        M - Interface type.
        Parameters:
        interfaceToMock - Class to mock.
        Returns:
        Mock object.
      • param

        public <T> T param​(T value)
        Wraps a parameter value when attempting to capture.
        Type Parameters:
        T - Value type.
        Parameters:
        value - Value.
        Returns:
        Value for parameter.
      • paramType

        public <T> T paramType​(java.lang.Class<T> type)
        Wraps a parameter type expected.
        Type Parameters:
        T - Value type.
        Parameters:
        type - Expected type.
        Returns:
        Value for parameter.
      • recordReturn

        public <T> void recordReturn​(java.lang.Object mockObject,
                                     T ignore,
                                     T recordedReturn)
        Convenience method to record a method and its return on a mock object.
        Type Parameters:
        T - Expected result type.
        Parameters:
        mockObject - Mock object.
        ignore - Result of operation on the mock object. This is only provided to obtain correct return type for recording return.
        recordedReturn - Value that is recorded to be returned from the mock object.
      • recordReturn

        public <T> void recordReturn​(java.lang.Object mockObject,
                                     T ignore,
                                     T recordedReturn,
                                     ArgumentsMatcher matcher)
        Convenience method to record a method, an ArgumentsMatcher and return value.
        Type Parameters:
        T - Expected result type.
        Parameters:
        mockObject - Mock object.
        ignore - Result of operation on the mock object. This is only provided to obtain correct return type for recording return.
        recordedReturn - Value that is recorded to be returned from the mock object.
        matcher - ArgumentsMatcher.
      • recordVoid

        public void recordVoid​(java.lang.Object mockObject,
                               ArgumentsMatcher matcher)
        Convenience method to record void method.
        Parameters:
        mockObject - Mock object.
        matcher - ArgumentsMatcher.
      • recordThrows

        public <T> void recordThrows​(java.lang.Object mockObject,
                                     T ignore,
                                     java.lang.Throwable exception)
        Convenience method to record an Exception.
        Type Parameters:
        T - Expected result type.
        Parameters:
        mockObject - Mock object.
        ignore - Result of operation on the mock object. This is only provided to obtain correct return type for recording return.
        exception - Throwable.
      • replayMockObjects

        public void replayMockObjects()
        Flags all the mock objects to replay.
      • verifyMockObjects

        public void verifyMockObjects()
        Verifies all mock objects.
      • doTest

        protected <R,​T extends java.lang.Throwable> R doTest​(MockTestSupport.TestLogic<R,​T> test)
                                                            throws T extends java.lang.Throwable
        Undertakes test wrapping with mock object replay and verify.
        Type Parameters:
        R - Return type of test logic.
        T - Possible Throwable.
        Parameters:
        test - Test logic to wrap in replay/verify.
        Returns:
        Result of test logic.
        Throws:
        T - If logic throws Exception.
        T extends java.lang.Throwable