Class ProxyMockMaker

  • All Implemented Interfaces:
    MockMaker

    public class ProxyMockMaker
    extends java.lang.Object
    implements MockMaker
    A mock maker that is using the Proxy utility and is therefore only capable of mocking interfaces but does not rely on manual byte code generation but only uses official and public Java API.
    • Field Detail

      • EMPTY

        private static final java.lang.Object[] EMPTY
      • invokeDefault

        private final java.lang.reflect.Method invokeDefault
    • Constructor Detail

      • ProxyMockMaker

        public ProxyMockMaker()
    • Method Detail

      • createMock

        public <T> T createMock​(MockCreationSettings<T> settings,
                                MockHandler handler)
        Description copied from interface: MockMaker
        If you want to provide your own implementation of MockMaker this method should:
        • Create a proxy object that implements settings.typeToMock and potentially also settings.extraInterfaces.
        • You may use the information from settings to create/configure your proxy object.
        • Your proxy object should carry the handler with it. For example, if you generate byte code to create the proxy you could generate an extra field to keep the handler with the generated object. Your implementation of MockMaker is required to provide this instance of handler when MockMaker.getHandler(Object) is called.
        Specified by:
        createMock in interface MockMaker
        Type Parameters:
        T - Type of the mock to return, actually the settings.getTypeToMock.
        Parameters:
        settings - Mock creation settings like type to mock, extra interfaces and so on.
        handler - See MockHandler. Do not provide your own implementation at this time. Make sure your implementation of MockMaker.getHandler(Object) will return this instance.
        Returns:
        The mock instance.
      • resetMock

        public void resetMock​(java.lang.Object mock,
                              MockHandler newHandler,
                              MockCreationSettings settings)
        Description copied from interface: MockMaker
        Replaces the existing handler on mock with newHandler.

        The invocation handler actually store invocations to achieve stubbing and verification. In order to reset the mock, we pass a new instance of the invocation handler.

        Your implementation should make sure the newHandler is correctly associated to passed mock

        Specified by:
        resetMock in interface MockMaker
        Parameters:
        mock - The mock instance whose invocation handler is to be replaced.
        newHandler - The new invocation handler instance.
        settings - The mock settings - should you need to access some of the mock creation details.
      • isTypeMockable

        public MockMaker.TypeMockability isTypeMockable​(java.lang.Class<?> type)
        Description copied from interface: MockMaker
        Indicates if the given type can be mocked by this mockmaker.

        Mockmaker may have different capabilities in term of mocking, typically Mockito 1.x's internal mockmaker cannot mock final types. Other implementations, may have different limitations.

        Specified by:
        isTypeMockable in interface MockMaker
        Parameters:
        type - The type inspected for mockability.
        Returns:
        object that carries the information about mockability of given type.