T
- type of the object being createdpublic interface IMockBuilder<T>
EasyMock
.
Example of usage:
public class MyClass { public MyClass(A a, B b) { } } public class MyClassTest { @Test public void testFoo() throws Exception { IMocksControl mockControl = createControl(); A a = mockControl.createMock(A.class); B b = mockControl.createMock(B.class); MyClass myClass = createMockBuilder(MyClass.class) .withConstructor(a, b).createMock(mockControl); // Set the expectations of A and B and test some method in MyClass } }
This class also has support for partial mocks as shown by the example below:
public class MyMockedClass {
// Empty class is also valid for IMockBuilder
.
public MyMockedClass() {
}
public void foo(int a) {
blah(a);
bleh();
}
public void blah(int a) {
}
public void bleh() {
}
}
public class MyMockedClassTest {
@Test
public void testFoo() throws Exception {
MyMockedClass myMockedClass = createMockBuilder(MyMockedClass.class)
.withConstructor().addMockedMethod("blah", int.class)
.addMockedMethod("bleh").createMock();
// These are the expectations.
myMockedClass.blah(1);
myMockedClass.bleh();
replay(myMockedClass);
myMockedClass.foo(1);
verify(myMockedClass);
}
}
Warning: There may be ambiguities when there are two different constructors with compatible types. For instance:
public class A { } public class B extends A { } public class ClassWithAmbiguity { public ClassWithAmbiguity(A a) { } public ClassWithAmbiguity(B b) { } }will cause problems if using
withConstructor(Object...)
. To solve
this, you can explicitly define the constructor parameter types to use by
calling withConstructor(Class...)
and then
withArgs(Object...)
, like this:
createMockBuilder(MyMockedClass.class).withConstructor(A.class).withArgs( new A()).createMock();
Modifier and Type | Method and Description |
---|---|
IMockBuilder<T> |
addMockedMethod(Method method)
Adds a method to be mocked in the testing class.
|
IMockBuilder<T> |
addMockedMethod(String methodName)
Adds a method to be mocked in the testing class.
|
IMockBuilder<T> |
addMockedMethod(String methodName,
Class<?>... parameterTypes)
Adds a method to be mocked in the testing class.
|
IMockBuilder<T> |
addMockedMethods(Method... methods)
Adds methods to be mocked in the testing class.
|
IMockBuilder<T> |
addMockedMethods(String... methodNames)
Adds methods to be mocked in the testing class.
|
<R> R |
createMock()
Create a default mock from this builder.
|
<R> R |
createMock(IMocksControl control)
Create mock from the provided mock control using the arguments passed to
the builder.
|
<R> R |
createMock(MockType type)
Create mock of the request type from this builder.
|
<R> R |
createMock(String name)
Create named mock from the provided mock control using the arguments
passed to the builder.
|
<R> R |
createMock(String name,
IMocksControl control)
Create named mock from the provided mock control using the arguments
passed to the builder.
|
<R> R |
createMock(String name,
MockType type)
Create a named mock of the request type from this builder.
|
<R> R |
createNiceMock()
Create a nice mock from this builder.
|
<R> R |
createNiceMock(String name)
Create a named nice mock from this builder.
|
<R> R |
createStrictMock()
Create a strict mock from this builder.
|
<R> R |
createStrictMock(String name)
Create a named strict mock from this builder.
|
default <R> R |
mock()
Create a default mock from this builder.
|
default <R> R |
mock(IMocksControl control)
Create mock from the provided mock control using the arguments passed to
the builder.
|
default <R> R |
mock(MockType type)
Create mock of the request type from this builder.
|
default <R> R |
mock(String name)
Create named mock from the provided mock control using the arguments
passed to the builder.
|
default <R> R |
mock(String name,
IMocksControl control)
Create named mock from the provided mock control using the arguments
passed to the builder.
|
default <R> R |
mock(String name,
MockType type)
Create a named mock of the request type from this builder.
|
default <R> R |
niceMock()
Create a nice mock from this builder.
|
default <R> R |
niceMock(String name)
Create a named nice mock from this builder.
|
default <R> R |
strictMock()
Create a strict mock from this builder.
|
default <R> R |
strictMock(String name)
Create a named strict mock from this builder.
|
IMockBuilder<T> |
withArgs(Object... initArgs)
Defines the arguments to be passed to the constructor of the class.
|
IMockBuilder<T> |
withConstructor()
Defines the empty constructor should be called.
|
IMockBuilder<T> |
withConstructor(Class<?>... argTypes)
Defines the exact argument types for the constructor to use.
|
IMockBuilder<T> |
withConstructor(Constructor<?> constructor)
Defines the constructor to use to instantiate the mock.
|
IMockBuilder<T> |
withConstructor(Object... initArgs)
Defines the constructor parameters for the mocked class.
|
IMockBuilder<T> addMockedMethod(Method method)
method
- method to be mockedIMockBuilder<T> addMockedMethod(String methodName)
addMockedMethod
s in this class if that is the case.methodName
- name of the method to be mockedIMockBuilder<T> addMockedMethod(String methodName, Class<?>... parameterTypes)
methodName
- name of the method to be mockedparameterTypes
- types of the parameters of the methodIMockBuilder<T> addMockedMethods(String... methodNames)
addMockedMethod(String)
but to mock many methods at once.methodNames
- names of the methods to be mockedIMockBuilder<T> addMockedMethods(Method... methods)
addMockedMethod(Method)
but to mock many methods at once.methods
- methods to be mockedIMockBuilder<T> withConstructor(Constructor<?> constructor)
withArgs(java.lang.Object...)
with the actual constructor argument
values after this.constructor
- the constructor to be calledIMockBuilder<T> withConstructor()
IMockBuilder<T> withConstructor(Object... initArgs)
initArgs
- arguments of the constructorIMockBuilder<T> withConstructor(Class<?>... argTypes)
withArgs(java.lang.Object...)
with the actual constructor
argument values after this.argTypes
- the exact argument types of the constructorIMockBuilder<T> withArgs(Object... initArgs)
withConstructor(Class...)
or
withConstructor(Constructor)
.initArgs
- the arguments to pass to the constructor<R> R createMock(MockType type)
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 mock type<R> R createStrictMock()
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<R> R createMock()
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<R> R createNiceMock()
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<R> R createMock(IMocksControl control)
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 typecontrol
- IMocksControl
used to create the object<R> R createMock(String name, MockType type)
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 mock nametype
- the mock type<R> R createStrictMock(String name)
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 mock name<R> R createMock(String name)
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 mock name<R> R createNiceMock(String name)
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 mock name<R> R createMock(String name, IMocksControl control)
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 mock namecontrol
- IMocksControl
used to create the objectdefault <R> R mock(MockType type)
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 mock typedefault <R> R strictMock()
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 typedefault <R> R mock()
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 typedefault <R> R niceMock()
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 typedefault <R> R mock(IMocksControl control)
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 typecontrol
- IMocksControl
used to create the objectdefault <R> R mock(String name, MockType type)
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 mock nametype
- the mock typedefault <R> R strictMock(String name)
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 mock namedefault <R> R mock(String name)
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 mock namedefault <R> R niceMock(String name)
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 mock namedefault <R> R mock(String name, IMocksControl control)
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 mock namecontrol
- IMocksControl
used to create the objectCopyright © 2001–2022 EasyMock contributors. All rights reserved.