EasyMockSupport (EasyMock 3.2 API)
Date: 2013-07-11

org.easymock

## Class EasyMockSupport

- java.lang.Object
- - org.easymock.EasyMockSupport

-     public class EasyMockSupport
      extends Object

  Helper class to be used to keep tracks of mocks easily. See EasyMock documentation and SupportTest sample.

  Example of usage:

       public class SupportTest extends EasyMockSupport {
           @Test
           public void test() {
               firstMock = createMock(A.class);
               secondMock = createMock(B.class);

               replayAll(); // put both mocks in replay mode

               // ... use mocks ..

               verifyAll(); // verify both mocks
           }
       }

  Author:
    Henri Tremblay

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected List<IMocksControl>` | `controls` List of all controls created

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `EasyMockSupport()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `IMocksControl` | `createControl()` Creates a control, order checking is disabled by default.
    `IMocksControl` | `createControl(MockType type)` Creates a control of the given type.
    `<T> T` | `createMock(Class<T> toMock)` Creates a mock object that implements the given interface, order checking is disabled by default.
    `<T> T` | `createMock(Class<T> toMock, ConstructorArgs constructorArgs, Method... mockedMethods)` Deprecated. Use `createMockBuilder(Class)` instead
    `<T> T` | `createMock(Class<T> toMock, Method... mockedMethods)` Deprecated. Use `createMockBuilder(Class)` instead
    `<T> T` | `createMock(MockType type, Class<T> toMock)` Creates a mock object of the requested type that implements the given interface or extends the given class
    `<T> T` | `createMock(String name, Class<T> toMock)` Creates a mock object that implements the given interface, order checking is disabled by default.
    `<T> T` | `createMock(String name, Class<T> toMock, ConstructorArgs constructorArgs, Method... mockedMethods)` Deprecated. Use `createMockBuilder(Class)` instead
    `<T> T` | `createMock(String name, Class<T> toMock, Method... mockedMethods)` Deprecated. Use `createMockBuilder(Class)` instead
    `<T> T` | `createMock(String name, MockType type, Class<T> toMock)` Creates a mock object of the requested type that implements the given interface or extends the given class
    `<T> IMockBuilder<T>` | `createMockBuilder(Class<T> toMock)` Create a mock builder allowing to create a partial mock for the given class or interface.
    `IMocksControl` | `createNiceControl()` Creates a control, order checking is disabled by default, and the mock objects created by this control will return `0`, `null` or `false` for unexpected invocations.
    `<T> T` | `createNiceMock(Class<T> toMock)` Creates a mock object that implements the given interface, order checking is disabled by default, and the mock object will return `0`, `null` or `false` for unexpected invocations.
    `<T> T` | `createNiceMock(Class<T> toMock, ConstructorArgs constructorArgs, Method... mockedMethods)` Deprecated. Use `createMockBuilder(Class)` instead
    `<T> T` | `createNiceMock(Class<T> toMock, Method... mockedMethods)` Deprecated. Use `createMockBuilder(Class)` instead
    `<T> T` | `createNiceMock(String name, Class<T> toMock)` Creates a mock object that implements the given interface, order checking is disabled by default, and the mock object will return `0`, `null` or `false` for unexpected invocations.
    `<T> T` | `createNiceMock(String name, Class<T> toMock, ConstructorArgs constructorArgs, Method... mockedMethods)` Deprecated. Use `createMockBuilder(Class)` instead
    `<T> T` | `createNiceMock(String name, Class<T> toMock, Method... mockedMethods)` Deprecated. Use `createMockBuilder(Class)` instead
    `IMocksControl` | `createStrictControl()` Creates a control, order checking is enabled by default.
    `<T> T` | `createStrictMock(Class<T> toMock)` Creates a mock object that implements the given interface, order checking is enabled by default.
    `<T> T` | `createStrictMock(Class<T> toMock, ConstructorArgs constructorArgs, Method... mockedMethods)` Deprecated. Use `createMockBuilder(Class)` instead
    `<T> T` | `createStrictMock(Class<T> toMock, Method... mockedMethods)` Deprecated. Use `createMockBuilder(Class)` instead
    `<T> T` | `createStrictMock(String name, Class<T> toMock)` Creates a mock object that implements the given interface, order checking is enabled by default.
    `<T> T` | `createStrictMock(String name, Class<T> toMock, ConstructorArgs constructorArgs, Method... mockedMethods)` Deprecated. Use `createMockBuilder(Class)` instead
    `<T> T` | `createStrictMock(String name, Class<T> toMock, Method... mockedMethods)` Deprecated. Use `createMockBuilder(Class)` instead
    `static void` | `injectMocks(Object obj)` Inject a mock to every fields annotated with `Mock` on the class passed in parameter.
    `void` | `replayAll()` Switches all registered mock objects (more exactly: the controls of the mock objects) to replay mode.
    `void` | `resetAll()` Resets all registered mock objects (more exactly: the controls of the mock objects).
    `void` | `resetAllToDefault()` Resets all registered mock objects (more exactly: the controls of the mock objects) and turn them to a mock with default behavior.
    `void` | `resetAllToNice()` Resets all registered mock objects (more exactly: the controls of the mock objects) and turn them to a mock with nice behavior.
    `void` | `resetAllToStrict()` Resets all registered mock objects (more exactly: the controls of the mock objects) and turn them to a mock with strict behavior.
    `void` | `verifyAll()` Verifies all registered mock objects (more exactly: the controls of the mock objects).

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### controls

          protected final List<IMocksControl> controls

      List of all controls created

  - ### Constructor Detail

    - #### EasyMockSupport

          public EasyMockSupport()

  - ### Method Detail

    - #### createStrictMock

          @Deprecated
          public <T> T createStrictMock(Class<T> toMock,
                                          Method... mockedMethods)

      Deprecated. Use `createMockBuilder(Class)` instead
      Creates a mock object that extends the given class, order checking is enabled by default.
      Type Parameters:
        `T` - the class that the mock object should extend.
      Parameters:
        `toMock` - the class that the mock object should extend.
        `mockedMethods` - methods that will be mocked, other methods will behave normally
      Returns:
        the mock object.

    - #### createStrictMock

          @Deprecated
          public <T> T createStrictMock(String name,
                                          Class<T> toMock,
                                          Method... mockedMethods)

      Deprecated. Use `createMockBuilder(Class)` instead
      Creates a mock object that extends the given class, order checking is enabled by default.
      Type Parameters:
        `T` - the class that the mock object should extend.
      Parameters:
        `name` - the name of the mock object.
        `toMock` - the class that the mock object should extend.
        `mockedMethods` - methods that will be mocked, other methods will behave normally
      Returns:
        the mock object.

    - #### createStrictMock

          @Deprecated
          public <T> T createStrictMock(Class<T> toMock,
                                          ConstructorArgs constructorArgs,
                                          Method... mockedMethods)

      Deprecated. Use `createMockBuilder(Class)` instead
      Creates a mock object that extends the given class, order checking is enabled by default.
      Type Parameters:
        `T` - the class that the mock object should extend.
      Parameters:
        `toMock` - the class that the mock object should extend.
        `constructorArgs` - constructor and parameters used to instantiate the mock.
        `mockedMethods` - methods that will be mocked, other methods will behave normally
      Returns:
        the mock object.

    - #### createStrictMock

          @Deprecated
          public <T> T createStrictMock(String name,
                                          Class<T> toMock,
                                          ConstructorArgs constructorArgs,
                                          Method... mockedMethods)

      Deprecated. Use `createMockBuilder(Class)` instead
      Creates a mock object that extends the given class, order checking is enabled by default.
      Type Parameters:
        `T` - the class that the mock object should extend.
      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.
        `mockedMethods` - methods that will be mocked, other methods will behave normally
      Returns:
        the mock object.

    - #### createMock

          @Deprecated
          public <T> T createMock(Class<T> toMock,
                                    Method... mockedMethods)

      Deprecated. Use `createMockBuilder(Class)` instead
      Creates a mock object that extends the given class, order checking is disabled by default.
      Type Parameters:
        `T` - the class that the mock object should extend.
      Parameters:
        `toMock` - the class that the mock object should extend.
        `mockedMethods` - methods that will be mocked, other methods will behave normally
      Returns:
        the mock object.

    - #### createMock

          @Deprecated
          public <T> T createMock(String name,
                                    Class<T> toMock,
                                    Method... mockedMethods)

      Deprecated. Use `createMockBuilder(Class)` instead
      Creates a mock object that extends the given class, order checking is disabled by default.
      Type Parameters:
        `T` - the class that the mock object should extend.
      Parameters:
        `name` - the name of the mock object.
        `toMock` - the class that the mock object should extend.
        `mockedMethods` - methods that will be mocked, other methods will behave normally
      Returns:
        the mock object.

    - #### createMock

          @Deprecated
          public <T> T createMock(Class<T> toMock,
                                    ConstructorArgs constructorArgs,
                                    Method... mockedMethods)

      Deprecated. Use `createMockBuilder(Class)` instead
      Creates a mock object that extends the given class, order checking is disabled by default.
      Type Parameters:
        `T` - the class that the mock object should extend.
      Parameters:
        `toMock` - the class that the mock object should extend.
        `constructorArgs` - constructor and parameters used to instantiate the mock.
        `mockedMethods` - methods that will be mocked, other methods will behave normally
      Returns:
        the mock object.

    - #### createMock

          @Deprecated
          public <T> T createMock(String name,
                                    Class<T> toMock,
                                    ConstructorArgs constructorArgs,
                                    Method... mockedMethods)

      Deprecated. Use `createMockBuilder(Class)` instead
      Creates a mock object that extends the given class, order checking is disabled by default.
      Type Parameters:
        `T` - the class that the mock object should extend.
      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.
        `mockedMethods` - methods that will be mocked, other methods will behave normally
      Returns:
        the mock object.

    - #### createNiceMock

          @Deprecated
          public <T> T createNiceMock(Class<T> toMock,
                                        Method... mockedMethods)

      Deprecated. Use `createMockBuilder(Class)` instead
      Creates a mock object that extends the given class, order checking is disabled by default, and the mock object will return `0`, `null` or `false` for unexpected invocations.
      Type Parameters:
        `T` - the class that the mock object should extend.
      Parameters:
        `toMock` - the class that the mock object should extend.
        `mockedMethods` - methods that will be mocked, other methods will behave normally
      Returns:
        the mock object.

    - #### createNiceMock

          @Deprecated
          public <T> T createNiceMock(String name,
                                        Class<T> toMock,
                                        Method... mockedMethods)

      Deprecated. Use `createMockBuilder(Class)` instead
      Creates a mock object that extends the given class, order checking is disabled by default, and the mock object will return `0`, `null` or `false` for unexpected invocations.
      Type Parameters:
        `T` - the class that the mock object should extend.
      Parameters:
        `name` - the name of the mock object.
        `toMock` - the class that the mock object should extend.
        `mockedMethods` - methods that will be mocked, other methods will behave normally
      Returns:
        the mock object.

    - #### createNiceMock

          @Deprecated
          public <T> T createNiceMock(Class<T> toMock,
                                        ConstructorArgs constructorArgs,
                                        Method... mockedMethods)

      Deprecated. Use `createMockBuilder(Class)` instead
      Creates a mock object that extends the given class, order checking is disabled by default, and the mock object will return `0`, `null` or `false` for unexpected invocations.
      Type Parameters:
        `T` - the class that the mock object should extend.
      Parameters:
        `toMock` - the class that the mock object should extend.
        `constructorArgs` - constructor and parameters used to instantiate the mock.
        `mockedMethods` - methods that will be mocked, other methods will behave normally
      Returns:
        the mock object.

    - #### createNiceMock

          @Deprecated
          public <T> T createNiceMock(String name,
                                        Class<T> toMock,
                                        ConstructorArgs constructorArgs,
                                        Method... mockedMethods)

      Deprecated. Use `createMockBuilder(Class)` instead
      Creates a mock object that extends the given class, order checking is disabled by default, and the mock object will return `0`, `null` or `false` for unexpected invocations.
      Type Parameters:
        `T` - the class that the mock object should extend.
      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.
        `mockedMethods` - methods that will be mocked, other methods will behave normally
      Returns:
        the mock object.

    - #### createMock

          public <T> T createMock(MockType type,
                         Class<T> toMock)

      Creates a mock object of the requested type that implements the given interface or extends the given class
      Type Parameters:
        `T` - the interface that the mock object should implement.
      Parameters:
        `type` - the type of the mock to be created.
        `toMock` - the class of the interface that the mock object should implement.
      Returns:
        the mock object.
      Throws:
        `IllegalArgumentException` - if the name is not a valid Java identifier.

    - #### createMock

          public <T> T createMock(String name,
                         MockType type,
                         Class<T> toMock)

      Creates a mock object of the requested type that implements the given interface or extends the given class
      Type Parameters:
        `T` - the interface that the mock object should implement.
      Parameters:
        `name` - the name of the mock object.
        `type` - the type of the mock to be created.
        `toMock` - the class of the interface that the mock object should implement.
      Returns:
        the mock object.
      Throws:
        `IllegalArgumentException` - if the name is not a valid Java identifier.

    - #### createStrictMock

          public <T> T createStrictMock(Class<T> toMock)

      Creates a mock object that implements the given interface, order checking is enabled by default.
      Type Parameters:
        `T` - the interface that the mock object should implement.
      Parameters:
        `toMock` - the class of the interface that the mock object should implement.
      Returns:
        the mock object.

    - #### createStrictMock

          public <T> T createStrictMock(String name,
                               Class<T> toMock)

      Creates a mock object that implements the given interface, order checking is enabled by default.
      Type Parameters:
        `T` - the interface that the mock object should implement.
      Parameters:
        `name` - the name of the mock object.
        `toMock` - the class of the interface that the mock object should implement.
      Returns:
        the mock object.
      Throws:
        `IllegalArgumentException` - if the name is not a valid Java identifier.

    - #### createMock

          public <T> T createMock(Class<T> toMock)

      Creates a mock object that implements the given interface, order checking is disabled by default.
      Type Parameters:
        `T` - the interface that the mock object should implement.
      Parameters:
        `toMock` - the class of the interface that the mock object should implement.
      Returns:
        the mock object.

    - #### createMock

          public <T> T createMock(String name,
                         Class<T> toMock)

      Creates a mock object that implements the given interface, order checking is disabled by default.
      Type Parameters:
        `T` - the interface that the mock object should implement.
      Parameters:
        `name` - the name of the mock object.
        `toMock` - the class of the interface that the mock object should implement.
      Returns:
        the mock object.
      Throws:
        `IllegalArgumentException` - if the name is not a valid Java identifier.

    - #### createNiceMock

          public <T> T createNiceMock(Class<T> toMock)

      Creates a mock object that implements the given interface, order checking is disabled by default, and the mock object will return `0`, `null` or `false` for unexpected invocations.
      Type Parameters:
        `T` - the interface that the mock object should implement.
      Parameters:
        `toMock` - the class of the interface that the mock object should implement.
      Returns:
        the mock object.

    - #### createNiceMock

          public <T> T createNiceMock(String name,
                             Class<T> toMock)

      Creates a mock object that implements the given interface, order checking is disabled by default, and the mock object will return `0`, `null` or `false` for unexpected invocations.
      Type Parameters:
        `T` - the interface that the mock object should implement.
      Parameters:
        `name` - the name of the mock object.
        `toMock` - the class of the interface that the mock object should implement.
      Returns:
        the mock object.
      Throws:
        `IllegalArgumentException` - if the name is not a valid Java identifier.

    - #### createStrictControl

          public IMocksControl createStrictControl()

      Creates a control, order checking is enabled by default.
      Returns:
        the control.

    - #### createMockBuilder

          public <T> IMockBuilder<T> createMockBuilder(Class<T> toMock)

      Create a mock builder allowing to create a partial mock for the given class or interface.
      Type Parameters:
        `T` - the interface that the mock object should implement.
      Parameters:
        `toMock` - the class of the interface that the mock object should implement.
      Returns:
        a mock builder to create a partial mock

    - #### createControl

          public IMocksControl createControl(MockType type)

      Creates a control of the given type.
      Parameters:
        `type` - the mock type.
      Returns:
        the control.
      Since:
        3.2

    - #### createControl

          public IMocksControl createControl()

      Creates a control, order checking is disabled by default.
      Returns:
        the control.

    - #### createNiceControl

          public IMocksControl createNiceControl()

      Creates a control, order checking is disabled by default, and the mock objects created by this control will return `0`, `null` or `false` for unexpected invocations.
      Returns:
        the control.

    - #### replayAll

          public void replayAll()

      Switches all registered mock objects (more exactly: the controls of the mock objects) to replay mode. For details, see the EasyMock documentation.

    - #### resetAll

          public void resetAll()

      Resets all registered mock objects (more exactly: the controls of the mock objects). For details, see the EasyMock documentation.

    - #### verifyAll

          public void verifyAll()

      Verifies all registered mock objects (more exactly: the controls of the mock objects).

    - #### resetAllToNice

          public void resetAllToNice()

      Resets all registered mock objects (more exactly: the controls of the mock objects) and turn them to a mock with nice behavior. For details, see the EasyMock documentation.

    - #### resetAllToDefault

          public void resetAllToDefault()

      Resets all registered mock objects (more exactly: the controls of the mock objects) and turn them to a mock with default behavior. For details, see the EasyMock documentation.

    - #### resetAllToStrict

          public void resetAllToStrict()

      Resets all registered mock objects (more exactly: the controls of the mock objects) and turn them to a mock with strict behavior. For details, see the EasyMock documentation.

    - #### injectMocks

          public static void injectMocks(Object obj)

      Inject a mock to every fields annotated with `Mock` on the class passed in parameter. Then, inject these mocks to the fields of every class annotated with `TestSubject`.

      The rules are

      - Static and final fields are ignored
      - If a mock can be assigned to a field, do it. The same mock an be assigned more than once
      - If no mock can be assigned to a field, skip it silently
      - If two mocks can be assigned to the same field, return an error
      Fields are searched recursively on the superclasses

      Note: If the parameter extends `EasyMockSupport`, the mocks will be created using it to allow `replayAll/verifyAll` to work afterwards

      Parameters:
        `obj` - the object on which to inject mocks
      Since:
        3.2

Copyright © 2001-2013 OFFIS, Tammo Freese, Henri Tremblay. All Rights Reserved.