Class MockBuilder<T>

  • Type Parameters:
    T - type of the mock created
    All Implemented Interfaces:
    IMockBuilder<T>

    public class MockBuilder<T>
    extends java.lang.Object
    implements IMockBuilder<T>
    Default implementation of IMockBuilder.

    The original idea and part of the code where contributed by Rodrigo Damazio and Bruno Fonseca at Google

    Author:
    Henri Tremblay
    • Constructor Detail

      • MockBuilder

        public MockBuilder​(java.lang.Class<T> toMock)
      • MockBuilder

        public MockBuilder​(java.lang.Class<T> toMock,
                           EasyMockSupport support)
        Used by EasyMockSupport to allow the mock registration in the list of controls
        Parameters:
        toMock - The class of the mock to create
        support - The EasyMockSupport used to create mocks. Null if none
    • Method Detail

      • addMockedMethod

        public IMockBuilder<T> addMockedMethod​(java.lang.reflect.Method method)
        Description copied from interface: IMockBuilder
        Adds a method to be mocked in the testing class. Each call will add a new method to the result mock. The method is searched for in the class itself as well as superclasses.
        Specified by:
        addMockedMethod in interface IMockBuilder<T>
        Parameters:
        method - method to be mocked
        Returns:
        this
      • addMockedMethod

        public IMockBuilder<T> addMockedMethod​(java.lang.String methodName)
        Description copied from interface: IMockBuilder
        Adds a method to be mocked in the testing class. Each call will add a new method to the result mock. The method is searched for in the class itself as well as superclasses. There must be no overload of the method. You will have to rely on the other addMockedMethods in this class if that is the case.
        Specified by:
        addMockedMethod in interface IMockBuilder<T>
        Parameters:
        methodName - name of the method to be mocked
        Returns:
        this
      • addMockedMethod

        public IMockBuilder<T> addMockedMethod​(java.lang.String methodName,
                                               java.lang.Class<?>... parameterTypes)
        Description copied from interface: IMockBuilder
        Adds a method to be mocked in the testing class. Each call will add a new method to the result mock. The method is searched for in the class itself as well as superclasses.
        Specified by:
        addMockedMethod in interface IMockBuilder<T>
        Parameters:
        methodName - name of the method to be mocked
        parameterTypes - types of the parameters of the method
        Returns:
        this
      • withConstructor

        public IMockBuilder<T> withConstructor​(java.lang.Object... initArgs)
        Description copied from interface: IMockBuilder
        Defines the constructor parameters for the mocked class. The builder will automatically find a constructor with compatible argument types. This throws an exception if there is more than one constructor which would accept the given parameters.
        Specified by:
        withConstructor in interface IMockBuilder<T>
        Parameters:
        initArgs - arguments of the constructor
        Returns:
        this
      • createMock

        public T createMock​(MockType type)
        Description copied from interface: IMockBuilder
        Create mock of the request type from this builder. The same builder can be called to create multiple mocks.
        Specified by:
        createMock in interface IMockBuilder<T>
        Parameters:
        type - the mock type
        Returns:
        the newly created mock
      • createMock

        public T createMock​(java.lang.String name,
                            MockType type)
        Description copied from interface: IMockBuilder
        Create a named mock of the request type from this builder. The same builder can be called to create multiple mocks.
        Specified by:
        createMock in interface IMockBuilder<T>
        Parameters:
        name - the mock name
        type - the mock type
        Returns:
        the newly created mock
      • createMock

        public T createMock​(IMocksControl control)
        Description copied from interface: IMockBuilder
        Create mock from the provided mock control using the arguments passed to the builder.
        Specified by:
        createMock in interface IMockBuilder<T>
        Parameters:
        control - IMocksControl used to create the object
        Returns:
        the newly created mock
      • createMock

        public T createMock()
        Description copied from interface: IMockBuilder
        Create a default mock from this builder. The same builder can be called to create multiple mocks.
        Specified by:
        createMock in interface IMockBuilder<T>
        Returns:
        the newly created mock
      • createNiceMock

        public T createNiceMock()
        Description copied from interface: IMockBuilder
        Create a nice mock from this builder. The same builder can be called to create multiple mocks.
        Specified by:
        createNiceMock in interface IMockBuilder<T>
        Returns:
        the newly created mock
      • createStrictMock

        public T createStrictMock()
        Description copied from interface: IMockBuilder
        Create a strict mock from this builder. The same builder can be called to create multiple mocks.
        Specified by:
        createStrictMock in interface IMockBuilder<T>
        Returns:
        the newly created mock
      • createMock

        public T createMock​(java.lang.String name,
                            IMocksControl control)
        Description copied from interface: IMockBuilder
        Create named mock from the provided mock control using the arguments passed to the builder.
        Specified by:
        createMock in interface IMockBuilder<T>
        Parameters:
        name - the mock name
        control - IMocksControl used to create the object
        Returns:
        the newly created mock
      • createMock

        public T createMock​(java.lang.String name)
        Description copied from interface: IMockBuilder
        Create named mock from the provided mock control using the arguments passed to the builder.
        Specified by:
        createMock in interface IMockBuilder<T>
        Parameters:
        name - the mock name
        Returns:
        the newly created mock
      • createNiceMock

        public T createNiceMock​(java.lang.String name)
        Description copied from interface: IMockBuilder
        Create a named nice mock from this builder. The same builder can be called to create multiple mocks.
        Specified by:
        createNiceMock in interface IMockBuilder<T>
        Parameters:
        name - the mock name
        Returns:
        the newly created mock
      • createStrictMock

        public T createStrictMock​(java.lang.String name)
        Description copied from interface: IMockBuilder
        Create a named strict mock from this builder. The same builder can be called to create multiple mocks.
        Specified by:
        createStrictMock in interface IMockBuilder<T>
        Parameters:
        name - the mock name
        Returns:
        the newly created mock