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 |
|---|---|
| public IMockBuilder | Returns: thismethod to be mocked method)Adds a method to be mocked in the testing class. |
| public IMockBuilder | Returns: thisname of the method to be mocked methodName)Adds a method to be mocked in the testing class. |
| public IMockBuilder | Returns: thisname of the method to be mocked methodName, Class<?>... types of the parameters of the method parameterTypes)Adds a method to be mocked in the testing class. |
| public IMockBuilder | Returns: thisnames of the methods to be mocked methodNames)Adds methods to be mocked in the testing class. |
| public IMockBuilder | Returns: thismethods to be mocked methods)Adds methods to be mocked in the testing class. |
| public < 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 newly created mockthe mock type type)Create mock of the request type from this builder. |
| public < 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 | |
| public < 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 newly created mockorg. used to create the objectCreate mock from the provided mock control using the arguments passed to the builder. |
| public < 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 newly created mockthe mock name name, MockType the mock type type)Create a named mock of the request type from this builder. |
| public < 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 newly created mockthe mock name name)Create named mock from the provided mock control using the arguments passed to the builder. |
| public < 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 newly created mockthe mock name name, IMocksControl org. used to create the objectCreate named mock from the provided mock control using the arguments passed to the builder. |
| public < 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 | |
| public < 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 newly created mockthe mock name name)Create a named nice mock from this builder. |
| public < 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 | |
| public < 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 newly created mockthe mock name name)Create a named strict mock from this builder. |
| public default < 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 | |
| public default < 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 | |
| public default < 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 newly created mockorg. used to create the objectCreate mock from the provided mock control using the arguments passed to the builder. |
| public default < 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 | |
| public default < 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 | |
| public default < 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 newly created mockthe mock name name, IMocksControl org. used to create the objectCreate named mock from the provided mock control using the arguments passed to the builder. |
| public default < 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 | |
| public default < 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 | |
| public default < 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 | |
| public default < 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 newly created mockthe mock name name)Create a named strict mock from this builder. |
| public IMockBuilder | |
| public IMockBuilder | Returns: thisthe constructor to be called constructor)Defines the constructor to use to instantiate the mock. |
| public IMockBuilder | |
| public IMockBuilder | Returns: thisarguments of the constructor initArgs)Defines the constructor parameters for the mocked class. |
| public IMockBuilder | Returns: thisthe exact argument types of the constructor argTypes)Defines the exact argument types for the constructor to use. |
| addMockedMethod | back to summary |
|---|---|
| public 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.
| |
| addMockedMethod | back to summary |
|---|---|
| public 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
| |
| addMockedMethod | back to summary |
|---|---|
| public 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.
| |
| addMockedMethods | back to summary |
|---|---|
| public IMockBuilder Adds methods to be mocked in the testing class. Same as
| |
| addMockedMethods | back to summary |
|---|---|
| public IMockBuilder Adds methods to be mocked in the testing class. Same as
| |
| createMock | back to summary |
|---|---|
| public <R> R createMock(MockType type) Create mock of the request type from this builder. The same builder can be called to create multiple mocks.
| |
| createMock | back to summary |
|---|---|
| public <R> R createMock() Create a default mock from this builder. The same builder can be called to create multiple mocks.
| |
| createMock | back to summary |
|---|---|
| public <R> R createMock(IMocksControl control) Create mock from the provided mock control using the arguments passed to the builder.
| |
| createMock | back to summary |
|---|---|
| public <R> R createMock(String name, MockType type) Create a named mock of the request type from this builder. The same builder can be called to create multiple mocks. | |
| createMock | back to summary |
|---|---|
| public <R> R createMock(String name) Create named mock from the provided mock control using the arguments passed to the builder.
| |
| createMock | back to summary |
|---|---|
| public <R> R createMock(String name, IMocksControl control) Create named mock from the provided mock control using the arguments passed to the builder.
| |
| createNiceMock | back to summary |
|---|---|
| public <R> R createNiceMock() Create a nice mock from this builder. The same builder can be called to create multiple mocks.
| |
| createNiceMock | back to summary |
|---|---|
| public <R> R createNiceMock(String name) Create a named nice mock from this builder. The same builder can be called to create multiple mocks.
| |
| createStrictMock | back to summary |
|---|---|
| public <R> R createStrictMock() Create a strict mock from this builder. The same builder can be called to create multiple mocks.
| |
| createStrictMock | back to summary |
|---|---|
| public <R> R createStrictMock(String name) Create a named strict mock from this builder. The same builder can be called to create multiple mocks.
| |
| mock | back to summary |
|---|---|
| public default <R> R mock(MockType type) Create mock of the request type from this builder. The same builder can be called to create multiple mocks.
| |
| mock | back to summary |
|---|---|
| public default <R> R mock() Create a default mock from this builder. The same builder can be called to create multiple mocks.
| |
| mock | back to summary |
|---|---|
| public default <R> R mock(IMocksControl control) Create mock from the provided mock control using the arguments passed to the builder.
| |
| mock | back to summary |
|---|---|
| public default <R> R mock(String name, MockType type) Create a named mock of the request type from this builder. The same builder can be called to create multiple mocks. | |
| mock | back to summary |
|---|---|
| public default <R> R mock(String name) Create named mock from the provided mock control using the arguments passed to the builder.
| |
| mock | back to summary |
|---|---|
| public default <R> R mock(String name, IMocksControl control) Create named mock from the provided mock control using the arguments passed to the builder.
| |
| niceMock | back to summary |
|---|---|
| public default <R> R niceMock() Create a nice mock from this builder. The same builder can be called to create multiple mocks.
| |
| niceMock | back to summary |
|---|---|
| public default <R> R niceMock(String name) Create a named nice mock from this builder. The same builder can be called to create multiple mocks.
| |
| strictMock | back to summary |
|---|---|
| public default <R> R strictMock() Create a strict mock from this builder. The same builder can be called to create multiple mocks.
| |
| strictMock | back to summary |
|---|---|
| public default <R> R strictMock(String name) Create a named strict mock from this builder. The same builder can be called to create multiple mocks.
| |
| withArgs | back to summary |
|---|---|
| public IMockBuilder Defines the arguments to be passed to the constructor of the class. The
types of the arguments must match those previously defined with
| |
| withConstructor | back to summary |
|---|---|
| public IMockBuilder Defines the constructor to use to instantiate the mock. It is expected
that you call
| |
| withConstructor | back to summary |
|---|---|
| public IMockBuilder Defines the empty constructor should be called.
| |
| withConstructor | back to summary |
|---|---|
| public 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.
| |
| withConstructor | back to summary |
|---|---|
| public IMockBuilder Defines the exact argument types for the constructor to use. It is
expected that you call
| |