Top Description Inners Fields Constructors Methods
org.easymock

public Class EasyMockSupport

extends Object
Class Inheritance
Imports
org.easymock.internal.Injector, .MockBuilder, .MocksControl, .ObjectMethodsFilter, .ReflectionUtils, java.lang.reflect.Proxy, java.util.ArrayList, .List

Helper class to keep track 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

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
EasyMockSupport.ObjectMockingHelper

Hides ByteBuddy classes from EasyMockSupport to prevent NoClassDefFoundError if ByteBuddy isn't used.

Field Summary

Modifier and TypeField and Description
protected final List<IMocksControl>
controls

List of all controls created

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public IMocksControl

Returns:

the control.
createControl
(MockType
the mock type.
type
)

Creates a control of the given type.

public IMocksControl

Returns:

the control.
createControl
()

Creates a control, order checking is disabled by default.

public <
the interface that the mock object should implement.
T
,
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
R
>
R

Returns:

the mock object.
createMock
(MockType
the type of the mock to be created.
type
,
Class<T>
the class of the interface that the mock object should implement.
toMock
)

Creates a mock object of the requested type that implements the given interface or extends the given class

public <
the interface that the mock object should implement.
T
,
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
R
>
R

Returns:

the mock object.
createMock
(String
the name of the mock object.
name
,
MockType
the type of the mock to be created.
type
,
Class<T>
the class of the interface that the mock object should implement.
toMock
)

Creates a mock object of the requested type that implements the given interface or extends the given class

public <
the interface that the mock object should implement.
T
,
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
R
>
R

Returns:

the mock object.
createMock
(Class<T>
the class of the interface that the mock object should implement.
toMock
)

Creates a mock object that implements the given interface, order checking is disabled by default.

public <
the interface that the mock object should implement.
T
,
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
R
>
R

Returns:

the mock object.
createMock
(String
the name of the mock object.
name
,
Class<T>
the class of the interface that the mock object should implement.
toMock
)

Creates a mock object that implements the given interface, order checking is disabled by default.

public <
the interface that the mock object should implement.
T
>
IMockBuilder<T>

Returns:

a mock builder to create a partial mock
createMockBuilder
(Class<T>
the class of the interface that the mock object should implement.
toMock
)

Create a mock builder allowing to create a partial mock for the given class or interface.

public IMocksControl

Returns:

the control.
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.

public <
the interface that the mock object should implement.
T
,
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
R
>
R

Returns:

the mock object.
createNiceMock
(Class<T>
the class of the interface that the mock object should implement.
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.

public <
the interface that the mock object should implement.
T
,
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
R
>
R

Returns:

the mock object.
createNiceMock
(String
the name of the mock object.
name
,
Class<T>
the class of the interface that the mock object should implement.
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.

public IMocksControl

Returns:

the control.
createStrictControl
()

Creates a control, order checking is enabled by default.

public <
the interface that the mock object should implement.
T
,
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
R
>
R

Returns:

the mock object.
createStrictMock
(Class<T>
the class of the interface that the mock object should implement.
toMock
)

Creates a mock object that implements the given interface, order checking is enabled by default.

public <
the interface that the mock object should implement.
T
,
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
R
>
R

Returns:

the mock object.
createStrictMock
(String
the name of the mock object.
name
,
Class<T>
the class of the interface that the mock object should implement.
toMock
)

Creates a mock object that implements the given interface, order checking is enabled by default.

public static <
type of the possible mock
T
,
type of mocked class
R extends T
>
Class<R>

Returns:

the mocked type or null of not a mock
getMockedClass
(T
mock we want the type of
possibleMock
)

Will return the class that was mocked if it's a mock or null otherwise.

public static void
injectMocks(Object
the object on which to inject mocks
obj
)

Inject a mock to every fields annotated with Mock on the class passed in parameter.

public static boolean

Returns:

true if it's a mock
isAMock
(Object
the object that might be a mock
possibleMock
)

Tells if this mock is an EasyMock mock.

public <
the interface that the mock object should implement.
T
,
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
R
>
R

Returns:

the mock object.
mock
(Class<T>
the class of the interface that the mock object should implement.
toMock
)

Creates a mock object that implements the given interface, order checking is disabled by default.

public <
the interface that the mock object should implement.
T
,
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
R
>
R

Returns:

the mock object.
mock
(String
the name of the mock object.
name
,
Class<T>
the class of the interface that the mock object should implement.
toMock
)

Creates a mock object that implements the given interface, order checking is disabled by default.

public <
the interface that the mock object should implement.
T
,
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
R
>
R

Returns:

the mock object.
mock
(MockType
the type of the mock to be created.
type
,
Class<T>
the class of the interface that the mock object should implement.
toMock
)

Creates a mock object of the requested type that implements the given interface or extends the given class

public <
the interface that the mock object should implement.
T
,
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
R
>
R

Returns:

the mock object.
mock
(String
the name of the mock object.
name
,
MockType
the type of the mock to be created.
type
,
Class<T>
the class of the interface that the mock object should implement.
toMock
)

Creates a mock object of the requested type that implements the given interface or extends the given class

public <
the interface that the mock object should implement.
T
,
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
R
>
R

Returns:

the mock object.
niceMock
(Class<T>
the class of the interface that the mock object should implement.
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.

public <
the interface that the mock object should implement.
T
,
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
R
>
R

Returns:

the mock object.
niceMock
(String
the name of the mock object.
name
,
Class<T>
the class of the interface that the mock object should implement.
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.

public <
the interface that the mock object should implement.
T
>
IMockBuilder<T>

Returns:

a mock builder to create a partial mock
partialMockBuilder
(Class<T>
the class of the interface that the mock object should implement.
toMock
)

Create a mock builder allowing to create a partial mock for the given class or interface.

public void
replayAll()

Switches all registered mock objects (more exactly: the controls of the mock objects) to replay mode.

public void
resetAll()

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

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.

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.

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.

public <
the interface that the mock object should implement.
T
,
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
R
>
R

Returns:

the mock object.
strictMock
(Class<T>
the class of the interface that the mock object should implement.
toMock
)

Creates a mock object that implements the given interface, order checking is enabled by default.

public <
the interface that the mock object should implement.
T
,
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
R
>
R

Returns:

the mock object.
strictMock
(String
the name of the mock object.
name
,
Class<T>
the class of the interface that the mock object should implement.
toMock
)

Creates a mock object that implements the given interface, order checking is enabled by default.

public void
verifyAll()

Verifies all registered mock objects have their expectations met and that no unexpected call was performed.

public void
verifyAllRecordings()

Verifies all registered mock objects have their expectations met.

public void
verifyAllUnexpectedCalls()

Verifies that no registered mock objects had unexpected calls.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

controlsback to summary
protected final List<IMocksControl> controls

List of all controls created

Constructor Detail

EasyMockSupportback to summary
public EasyMockSupport()

Method Detail

createControlback to summary
public IMocksControl createControl(MockType type)

Creates a control of the given type.

Parameters
type:MockType

the mock type.

Returns:IMocksControl

the control.

Since
3.2
createControlback to summary
public IMocksControl createControl()

Creates a control, order checking is disabled by default.

Returns:IMocksControl

the control.

createMockback to summary
public <T, R> R createMock(MockType type, Class<T> toMock)

Creates a mock object of the requested type that implements the given interface or extends the given class

Parameters
<T>
the interface that the mock object should implement.
<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
type:MockType

the type of the mock to be created.

toMock:Class<T>

the class of the interface that the mock object should implement.

Returns:R

the mock object.

Exceptions
IllegalArgumentException:
if the name is not a valid Java identifier.
createMockback to summary
public <T, R> R 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

Parameters
<T>
the interface that the mock object should implement.
<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
name:String

the name of the mock object.

type:MockType

the type of the mock to be created.

toMock:Class<T>

the class of the interface that the mock object should implement.

Returns:R

the mock object.

Exceptions
IllegalArgumentException:
if the name is not a valid Java identifier.
createMockback to summary
public <T, R> R createMock(Class<T> toMock)

Creates a mock object that implements the given interface, order checking is disabled by default.

Parameters
<T>
the interface that the mock object should implement.
<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
toMock:Class<T>

the class of the interface that the mock object should implement.

Returns:R

the mock object.

createMockback to summary
public <T, R> R createMock(String name, Class<T> toMock)

Creates a mock object that implements the given interface, order checking is disabled by default.

Parameters
<T>
the interface that the mock object should implement.
<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
name:String

the name of the mock object.

toMock:Class<T>

the class of the interface that the mock object should implement.

Returns:R

the mock object.

Exceptions
IllegalArgumentException:
if the name is not a valid Java identifier.
createMockBuilderback to summary
public <T> IMockBuilder<T> createMockBuilder(Class<T> toMock)

Create a mock builder allowing to create a partial mock for the given class or interface.

Parameters
<T>
the interface that the mock object should implement.
toMock:Class<T>

the class of the interface that the mock object should implement.

Returns:IMockBuilder<T>

a mock builder to create a partial mock

createNiceControlback to summary
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:IMocksControl

the control.

createNiceMockback to summary
public <T, R> R 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.

Parameters
<T>
the interface that the mock object should implement.
<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
toMock:Class<T>

the class of the interface that the mock object should implement.

Returns:R

the mock object.

createNiceMockback to summary
public <T, R> R 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.

Parameters
<T>
the interface that the mock object should implement.
<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
name:String

the name of the mock object.

toMock:Class<T>

the class of the interface that the mock object should implement.

Returns:R

the mock object.

Exceptions
IllegalArgumentException:
if the name is not a valid Java identifier.
createStrictControlback to summary
public IMocksControl createStrictControl()

Creates a control, order checking is enabled by default.

Returns:IMocksControl

the control.

createStrictMockback to summary
public <T, R> R createStrictMock(Class<T> toMock)

Creates a mock object that implements the given interface, order checking is enabled by default.

Parameters
<T>
the interface that the mock object should implement.
<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
toMock:Class<T>

the class of the interface that the mock object should implement.

Returns:R

the mock object.

createStrictMockback to summary
public <T, R> R createStrictMock(String name, Class<T> toMock)

Creates a mock object that implements the given interface, order checking is enabled by default.

Parameters
<T>
the interface that the mock object should implement.
<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
name:String

the name of the mock object.

toMock:Class<T>

the class of the interface that the mock object should implement.

Returns:R

the mock object.

Exceptions
IllegalArgumentException:
if the name is not a valid Java identifier.
getMockedClassback to summary
public static <T, R extends T> Class<R> getMockedClass(T possibleMock)

Will return the class that was mocked if it's a mock or null otherwise.

Parameters
<T>
type of the possible mock
<R>
type of mocked class
possibleMock:T

mock we want the type of

Returns:Class<R>

the mocked type or null of not a mock

Since
3.5
injectMocksback to summary
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 two mocks have the same field name, return an error
  • If a mock has a field name and no matching field is found, return an error
Then, ignoring all fields and mocks matched by field name
  • If a mock without field name can be assigned to a field, do it. The same mock can be assigned more than once
  • If no mock can be assigned to a field, skip the field silently
  • If the mock cannot be assigned to any field, skip the mock 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:Object

the object on which to inject mocks

Since
3.2
isAMockback to summary
public static boolean isAMock(Object possibleMock)

Tells if this mock is an EasyMock mock.

Parameters
possibleMock:Object

the object that might be a mock

Returns:boolean

true if it's a mock

mockback to summary
public <T, R> R mock(Class<T> toMock)

Creates a mock object that implements the given interface, order checking is disabled by default.

Parameters
<T>
the interface that the mock object should implement.
<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
toMock:Class<T>

the class of the interface that the mock object should implement.

Returns:R

the mock object.

Since
${project.version}
mockback to summary
public <T, R> R mock(String name, Class<T> toMock)

Creates a mock object that implements the given interface, order checking is disabled by default.

Parameters
<T>
the interface that the mock object should implement.
<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
name:String

the name of the mock object.

toMock:Class<T>

the class of the interface that the mock object should implement.

Returns:R

the mock object.

Exceptions
IllegalArgumentException:
if the name is not a valid Java identifier.
Since
${project.version}
mockback to summary
public <T, R> R mock(MockType type, Class<T> toMock)

Creates a mock object of the requested type that implements the given interface or extends the given class

Parameters
<T>
the interface that the mock object should implement.
<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
type:MockType

the type of the mock to be created.

toMock:Class<T>

the class of the interface that the mock object should implement.

Returns:R

the mock object.

Exceptions
IllegalArgumentException:
if the name is not a valid Java identifier.
Since
${project.version}
mockback to summary
public <T, R> R mock(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

Parameters
<T>
the interface that the mock object should implement.
<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
name:String

the name of the mock object.

type:MockType

the type of the mock to be created.

toMock:Class<T>

the class of the interface that the mock object should implement.

Returns:R

the mock object.

Exceptions
IllegalArgumentException:
if the name is not a valid Java identifier.
Since
${project.version}
niceMockback to summary
public <T, R> R niceMock(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.

Parameters
<T>
the interface that the mock object should implement.
<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
toMock:Class<T>

the class of the interface that the mock object should implement.

Returns:R

the mock object.

Since
${project.version}
niceMockback to summary
public <T, R> R niceMock(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.

Parameters
<T>
the interface that the mock object should implement.
<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
name:String

the name of the mock object.

toMock:Class<T>

the class of the interface that the mock object should implement.

Returns:R

the mock object.

Exceptions
IllegalArgumentException:
if the name is not a valid Java identifier.
Since
${project.version}
partialMockBuilderback to summary
public <T> IMockBuilder<T> partialMockBuilder(Class<T> toMock)

Create a mock builder allowing to create a partial mock for the given class or interface.

Parameters
<T>
the interface that the mock object should implement.
toMock:Class<T>

the class of the interface that the mock object should implement.

Returns:IMockBuilder<T>

a mock builder to create a partial mock

Since
${project.version}
replayAllback to summary
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.

resetAllback to summary
public void resetAll()

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

resetAllToDefaultback to summary
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.

resetAllToNiceback to summary
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.

resetAllToStrictback to summary
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.

strictMockback to summary
public <T, R> R strictMock(Class<T> toMock)

Creates a mock object that implements the given interface, order checking is enabled by default.

Parameters
<T>
the interface that the mock object should implement.
<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
toMock:Class<T>

the class of the interface that the mock object should implement.

Returns:R

the mock object.

Since
${project.version}
strictMockback to summary
public <T, R> R strictMock(String name, Class<T> toMock)

Creates a mock object that implements the given interface, order checking is enabled by default.

Parameters
<T>
the interface that the mock object should implement.
<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
name:String

the name of the mock object.

toMock:Class<T>

the class of the interface that the mock object should implement.

Returns:R

the mock object.

Exceptions
IllegalArgumentException:
if the name is not a valid Java identifier.
Since
${project.version}
verifyAllback to summary
public void verifyAll()

Verifies all registered mock objects have their expectations met and that no unexpected call was performed.

This method as same effect as calling verifyAllRecordings() followed by verifyAllUnexpectedCalls().

verifyAllRecordingsback to summary
public void verifyAllRecordings()

Verifies all registered mock objects have their expectations met.

Since
3.5
verifyAllUnexpectedCallsback to summary
public void verifyAllUnexpectedCalls()

Verifies that no registered mock objects had unexpected calls.

Since
3.5
org.easymock back to summary

private Class EasyMockSupport.ObjectMockingHelper

extends Object
Class Inheritance

Hides ByteBuddy classes from EasyMockSupport to prevent NoClassDefFoundError if ByteBuddy isn't used.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static boolean
isAClassMock(Object possibleMock)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

ObjectMockingHelperback to summary
private ObjectMockingHelper()

Method Detail

isAClassMockback to summary
public static boolean isAClassMock(Object possibleMock)