public class EasyMockSupport extends Object
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 } }
Modifier and Type | Field and Description |
---|---|
protected List<IMocksControl> |
controls
List of all controls created
|
Constructor and Description |
---|
EasyMockSupport() |
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,R> R |
createMock(Class<T> toMock)
Creates a mock object that implements the given interface, order checking
is disabled by default.
|
<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
|
<T,R> R |
createMock(String name,
Class<T> toMock)
Creates a mock object that implements the given interface, order checking
is disabled by default.
|
<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
|
<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,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. |
<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. |
IMocksControl |
createStrictControl()
Creates a control, order checking is enabled by default.
|
<T,R> R |
createStrictMock(Class<T> toMock)
Creates a mock object that implements the given interface, order checking
is enabled by default.
|
<T,R> R |
createStrictMock(String name,
Class<T> toMock)
Creates a mock object that implements the given interface, order checking
is enabled by default.
|
static <T,R extends T> |
getMockedClass(T possibleMock)
Will return the class that was mocked if it's a mock or
null otherwise. |
static void |
injectMocks(Object obj)
Inject a mock to every fields annotated with
Mock on the class passed
in parameter. |
static boolean |
isAMock(Object possibleMock)
Tells if this mock is an EasyMock mock.
|
<T,R> R |
mock(Class<T> toMock)
Creates a mock object that implements the given interface, order checking
is disabled by default.
|
<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
|
<T,R> R |
mock(String name,
Class<T> toMock)
Creates a mock object that implements the given interface, order checking
is disabled by default.
|
<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
|
<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. |
<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. |
<T> IMockBuilder<T> |
partialMockBuilder(Class<T> toMock)
Create a mock builder allowing to create a partial mock for the given
class or interface.
|
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.
|
<T,R> R |
strictMock(Class<T> toMock)
Creates a mock object that implements the given interface, order checking
is enabled by default.
|
<T,R> R |
strictMock(String name,
Class<T> toMock)
Creates a mock object that implements the given interface, order checking
is enabled by default.
|
void |
verifyAll()
Verifies all registered mock objects have their expectations met and that no
unexpected call was performed.
|
void |
verifyAllRecordings()
Verifies all registered mock objects have their expectations met.
|
void |
verifyAllUnexpectedCalls()
Verifies that no registered mock objects had
unexpected calls.
|
protected final List<IMocksControl> controls
public <T,R> R mock(Class<T> toMock)
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 typetoMock
- the class of the interface that the mock object should
implement.public <T,R> R mock(String name, Class<T> toMock)
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 typename
- the name of the mock object.toMock
- the class of the interface that the mock object should
implement.IllegalArgumentException
- if the name is not a valid Java identifier.public <T,R> R mock(MockType type, Class<T> toMock)
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 typetype
- the type of the mock to be created.toMock
- the class of the interface that the mock object should
implement.IllegalArgumentException
- if the name is not a valid Java identifier.public <T,R> R mock(String name, MockType type, Class<T> toMock)
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 typename
- 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.IllegalArgumentException
- if the name is not a valid Java identifier.public <T,R> R strictMock(Class<T> toMock)
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 typetoMock
- the class of the interface that the mock object should
implement.public <T,R> R strictMock(String name, Class<T> toMock)
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 typename
- the name of the mock object.toMock
- the class of the interface that the mock object should
implement.IllegalArgumentException
- if the name is not a valid Java identifier.public <T,R> R niceMock(Class<T> toMock)
0
,
null
or false
for unexpected invocations.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 typetoMock
- the class of the interface that the mock object should
implement.public <T,R> R niceMock(String name, Class<T> toMock)
0
,
null
or false
for unexpected invocations.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 typename
- the name of the mock object.toMock
- the class of the interface that the mock object should
implement.IllegalArgumentException
- if the name is not a valid Java identifier.public <T> IMockBuilder<T> partialMockBuilder(Class<T> toMock)
T
- the interface that the mock object should implement.toMock
- the class of the interface that the mock object should
implement.public <T,R> R createMock(MockType type, Class<T> toMock)
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 typetype
- the type of the mock to be created.toMock
- the class of the interface that the mock object should
implement.IllegalArgumentException
- if the name is not a valid Java identifier.public <T,R> R createMock(String name, MockType type, Class<T> toMock)
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 typename
- 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.IllegalArgumentException
- if the name is not a valid Java identifier.public <T,R> R createStrictMock(Class<T> toMock)
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 typetoMock
- the class of the interface that the mock object should
implement.public <T,R> R createStrictMock(String name, Class<T> toMock)
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 typename
- the name of the mock object.toMock
- the class of the interface that the mock object should
implement.IllegalArgumentException
- if the name is not a valid Java identifier.public <T,R> R createMock(Class<T> toMock)
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 typetoMock
- the class of the interface that the mock object should
implement.public <T,R> R createMock(String name, Class<T> toMock)
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 typeT
- the interface that the mock object should implement.name
- the name of the mock object.toMock
- the class of the interface that the mock object should
implement.IllegalArgumentException
- if the name is not a valid Java identifier.public <T,R> R createNiceMock(Class<T> toMock)
0
,
null
or false
for unexpected invocations.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 typetoMock
- the class of the interface that the mock object should
implement.public <T,R> R createNiceMock(String name, Class<T> toMock)
0
,
null
or false
for unexpected invocations.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 typename
- the name of the mock object.toMock
- the class of the interface that the mock object should
implement.IllegalArgumentException
- if the name is not a valid Java identifier.public IMocksControl createStrictControl()
public <T> IMockBuilder<T> createMockBuilder(Class<T> toMock)
T
- the interface that the mock object should implement.toMock
- the class of the interface that the mock object should
implement.public IMocksControl createControl(MockType type)
type
- the mock type.public IMocksControl createControl()
public IMocksControl createNiceControl()
0
,
null
or false
for unexpected invocations.public void replayAll()
public void resetAll()
public void verifyAll()
This method as same effect as calling verifyAllRecordings()
followed by verifyAllUnexpectedCalls()
.
public void verifyAllRecordings()
public void verifyAllUnexpectedCalls()
public void resetAllToNice()
public void resetAllToDefault()
public void resetAllToStrict()
public static void injectMocks(Object obj)
Mock
on the class passed
in parameter. Then, inject these mocks to the fields of every class annotated with TestSubject
.
The rules are
Note: If the parameter extends EasyMockSupport
, the mocks will be created using it to allow
replayAll/verifyAll
to work afterwards
obj
- the object on which to inject mockspublic static <T,R extends T> Class<R> getMockedClass(T possibleMock)
null
otherwise.T
- type of the possible mockR
- type of mocked classpossibleMock
- mock we want the type ofpublic static boolean isAMock(Object possibleMock)
possibleMock
- the object that might be a mockCopyright © 2001–2022 EasyMock contributors. All rights reserved.