Class MocksControl

    • Field Detail

      • ONCE

        public static final Range ONCE
        Exactly one call.
      • AT_LEAST_ONCE

        public static final Range AT_LEAST_ONCE
        One or more calls.
      • ZERO_OR_MORE

        public static final Range ZERO_OR_MORE
        Zero or more calls.
      • classProxyFactory

        private static volatile IProxyFactory classProxyFactory
        lazily created; the proxy factory for classes
      • interfaceProxyFactory

        private static final IProxyFactory interfaceProxyFactory
    • Constructor Detail

      • MocksControl

        public MocksControl​(MockType type)
    • Method Detail

      • createMock

        public <T,​R> R createMock​(java.lang.Class<T> toMock)
        Description copied from interface: IMocksControl
        Same as IMocksControl.mock(Class) but using the old naming.
        Specified by:
        createMock in interface IMocksControl
        Type Parameters:
        T - the interface or class that the mock object should implement/extend.
        R - the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the caller, we return a different type
        Parameters:
        toMock - the interface or class that the mock object should implement/extend.
        Returns:
        the mock object.
      • createMock

        public <T,​R> R createMock​(java.lang.String name,
                                        java.lang.Class<T> toMock)
        Description copied from interface: IMocksControl
        Same as IMocksControl.mock(String, Class) but using the old naming.
        Specified by:
        createMock in interface IMocksControl
        Type Parameters:
        T - the interface or class that the mock object should implement/extend.
        R - the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the caller, we return a different type
        Parameters:
        name - the name of the mock object.
        toMock - the interface or class that the mock object should implement/extend.
        Returns:
        the mock object.
      • createMock

        public <T,​R> R createMock​(java.lang.String name,
                                        java.lang.Class<T> toMock,
                                        ConstructorArgs constructorArgs,
                                        java.lang.reflect.Method... mockedMethods)
        Description copied from interface: IMocksControl
        Specified by:
        createMock in interface IMocksControl
        Type Parameters:
        T - the class that the mock object should extend.
        R - the returned type. In general T == R but when mocking a generic type, it won't so to be nice with the caller, we return a different type
        Parameters:
        name - the name of the mock object.
        toMock - the class that the mock object should extend.
        constructorArgs - constructor and parameters used to instantiate the mock. If null, no constructor will be called
        mockedMethods - methods that will be mocked, other methods will behave normally. If empty, all methods will be mocked
        Returns:
        the mock object.
      • checkInterfaceHasDefaultMethodAndIsMockingOnlyThem

        private <T> void checkInterfaceHasDefaultMethodAndIsMockingOnlyThem​(java.lang.Class<T> toMock,
                                                                            java.lang.reflect.Method[] mockedMethods)
      • getProxyFactory

        public static IProxyFactory getProxyFactory​(java.lang.Object o)
      • getClassProxyFactory

        private static IProxyFactory getClassProxyFactory()
      • getControl

        public static MocksControl getControl​(java.lang.Object mock)
      • getInvocationHandler

        public static java.lang.reflect.InvocationHandler getInvocationHandler​(java.lang.Object mock)
      • getMockedClass

        public static <T,​R extends T> java.lang.Class<R> getMockedClass​(T proxy)
        Return the class of interface (depending on the mock type) that was mocked
        Type Parameters:
        T - Mocked class
        R - Mock class
        Parameters:
        proxy - Mock object
        Returns:
        the mocked class or interface
      • reset

        public void reset()
        Description copied from interface: IMocksControl
        Removes all expectations for the mock objects of this control.
        Specified by:
        reset in interface IMocksControl
      • resetToNice

        public void resetToNice()
        Description copied from interface: IMocksControl
        Removes all expectations for the mock objects of this control and turn them to nice mocks.
        Specified by:
        resetToNice in interface IMocksControl
      • resetToDefault

        public void resetToDefault()
        Description copied from interface: IMocksControl
        Removes all expectations for the mock objects of this control and turn them to default mocks.
        Specified by:
        resetToDefault in interface IMocksControl
      • resetToStrict

        public void resetToStrict()
        Description copied from interface: IMocksControl
        Removes all expectations for the mock objects of this control and turn them to strict mocks.
        Specified by:
        resetToStrict in interface IMocksControl
      • replay

        public void replay()
        Description copied from interface: IMocksControl
        Switches the control from record mode to replay mode.
        Specified by:
        replay in interface IMocksControl
      • checkOrder

        public void checkOrder​(boolean value)
        Description copied from interface: IMocksControl
        Switches order checking on and off.
        Specified by:
        checkOrder in interface IMocksControl
        Parameters:
        value - true switches order checking on, false switches it off.
      • makeThreadSafe

        public void makeThreadSafe​(boolean threadSafe)
        Description copied from interface: IMocksControl
        Makes the mock thread safe.
        Specified by:
        makeThreadSafe in interface IMocksControl
        Parameters:
        threadSafe - If the mock should be thread safe or not
      • checkIsUsedInOneThread

        public void checkIsUsedInOneThread​(boolean shouldBeUsedInOneThread)
        Description copied from interface: IMocksControl
        Check that the mock is called from only one thread
        Specified by:
        checkIsUsedInOneThread in interface IMocksControl
        Parameters:
        shouldBeUsedInOneThread - If it should be used in one thread only or not
      • andReturn

        public IExpectationSetters<java.lang.Object> andReturn​(java.lang.Object value)
        Description copied from interface: IExpectationSetters
        Sets a return value that will be returned for the expected invocation.
        Specified by:
        andReturn in interface IExpectationSetters<java.lang.Object>
        Parameters:
        value - the value to return.
        Returns:
        this object to allow method call chaining.
      • andThrow

        public IExpectationSetters<java.lang.Object> andThrow​(java.lang.Throwable throwable)
        Description copied from interface: IExpectationSetters
        Sets a throwable that will be thrown for the expected invocation.
        Specified by:
        andThrow in interface IExpectationSetters<java.lang.Object>
        Parameters:
        throwable - the throwable to throw.
        Returns:
        this object to allow method call chaining.
      • andAnswer

        public IExpectationSetters<java.lang.Object> andAnswer​(IAnswer<?> answer)
        Description copied from interface: IExpectationSetters
        Sets an object that will be used to calculate the answer for the expected invocation (either return a value, or throw an exception).
        Specified by:
        andAnswer in interface IExpectationSetters<java.lang.Object>
        Parameters:
        answer - the object used to answer the invocation.
        Returns:
        this object to allow method call chaining.
      • andDelegateTo

        public IExpectationSetters<java.lang.Object> andDelegateTo​(java.lang.Object answer)
        Description copied from interface: IExpectationSetters
        Sets an object implementing the same interface as the mock. The expected method call will be delegated to it with the actual arguments. The answer returned by this call will then be the answer returned by the mock (either return a value, or throw an exception).
        Specified by:
        andDelegateTo in interface IExpectationSetters<java.lang.Object>
        Parameters:
        answer - the object the call is delegated to.
        Returns:
        the value returned by the delegated call.
      • andVoid

        public IExpectationSetters<java.lang.Object> andVoid()
        Description copied from interface: IExpectationSetters
        Records a call but returns nothing. Used to chain calls on void methods expectLastCall().andThrow(e).andVoid()
        Specified by:
        andVoid in interface IExpectationSetters<java.lang.Object>
        Returns:
        this object to allow method call chaining.
      • andStubReturn

        public void andStubReturn​(java.lang.Object value)
        Description copied from interface: IExpectationSetters
        Sets a stub return value that will be returned for the expected invocation.
        Specified by:
        andStubReturn in interface IExpectationSetters<java.lang.Object>
        Parameters:
        value - the value to return.
      • andStubThrow

        public void andStubThrow​(java.lang.Throwable throwable)
        Description copied from interface: IExpectationSetters
        Sets a stub throwable that will be thrown for the expected invocation.
        Specified by:
        andStubThrow in interface IExpectationSetters<java.lang.Object>
        Parameters:
        throwable - the throwable to throw.
      • andStubAnswer

        public void andStubAnswer​(IAnswer<?> answer)
        Description copied from interface: IExpectationSetters
        Sets a stub object that will be used to calculate the answer for the expected invocation (either return a value, or throw an exception).
        Specified by:
        andStubAnswer in interface IExpectationSetters<java.lang.Object>
        Parameters:
        answer - the object used to answer the invocation.
      • andStubDelegateTo

        public void andStubDelegateTo​(java.lang.Object delegateTo)
        Description copied from interface: IExpectationSetters
        Sets a stub object implementing the same interface as the mock. The expected method call will be delegated to it with the actual arguments. The answer returned by this call will then be the answer returned by the mock (either return a value, or throw an exception).
        Specified by:
        andStubDelegateTo in interface IExpectationSetters<java.lang.Object>
        Parameters:
        delegateTo - the object the call is delegated to.
      • asStub

        public void asStub()
        Description copied from interface: IExpectationSetters
        Sets stub behavior for the expected invocation (this is needed for void methods).
        Specified by:
        asStub in interface IExpectationSetters<java.lang.Object>
      • times

        public IExpectationSetters<java.lang.Object> times​(int times)
        Description copied from interface: IExpectationSetters
        Expect the last invocation count times.
        Specified by:
        times in interface IExpectationSetters<java.lang.Object>
        Parameters:
        times - the number of invocations expected
        Returns:
        this object to allow method call chaining.
      • times

        public IExpectationSetters<java.lang.Object> times​(int min,
                                                           int max)
        Description copied from interface: IExpectationSetters
        Expect the last invocation between min and max times.
        Specified by:
        times in interface IExpectationSetters<java.lang.Object>
        Parameters:
        min - the minimum number of invocations expected.
        max - the maximum number of invocations expected.
        Returns:
        this object to allow method call chaining.