T - specifies the type (class, interface, etc.) to be mocked; multiple interfaces can be mocked by defining
a type variable in the test class or test method, and using it as the type argument;
if a type variable is used but it extends a single type, then all implementation classes extending/implementing that
base type are also mocked;
if the type argument itself is a parameterized type, then only its raw type is considered for mockingpublic abstract class MockUp<T> extends Object
// Define and apply one or more mock-ups:
new MockUp<SomeClass>() {
@Mock int someMethod(int i) { assertTrue(i > 0); return 123; }
@Mock(maxInvocations = 2) void anotherMethod(int i, String s) { /* validate arguments */ }
};
// Exercise code under test:
codeUnderTest.doSomething();
One or more mock methods annotated as such must be defined in the concrete subclass.
Each @Mock method should have a matching method or constructor in the mocked class/interface.
At runtime, the execution of a mocked method/constructor will get redirected to the corresponding mock method.
When mocking an interface, an implementation class is generated where all methods are empty, with non-void methods
returning a default value according to the return type: 0 for int, null for a reference
type, and so on.
When the type to be mocked is specified indirectly through a type variable, there are two
other possible outcomes:
extends" two or more interfaces, a mocked proxy class that implements all
interfaces is created, with the proxy instance made available through a call to getMockInstance().
Example:
@Test
public <M extends Runnable & ResultSet> void someTest() {
M mock = new MockUp<M>() {
@Mock void run() { ...do something... }
@Mock boolean next() { return true; }
}.getMockInstance();
mock.run();
assertTrue(mock.next());
}
@Test
public <BC extends SomeBaseClass> void someTest() {
new MockUp<BC>() {
@Mock int someMethod(int i) { return i + 1; }
};
int i = new AConcreteSubclass().someMethod(1);
assertEquals(2, i);
}
MockUp(),
MockUp(Class),
MockUp(Object),
getMockInstance(),
tearDown(),
Tutorial| Modifier | Constructor and Description |
|---|---|
protected |
MockUp()
Applies the mock methods defined in the concrete subclass to the class or interface specified
through the type parameter.
|
protected |
MockUp(Class<?> classToMock)
Applies the mock methods defined in the mock-up subclass to the given class/interface.
|
protected |
MockUp(T instanceToMock)
Applies the mock methods defined in the mock-up subclass to the type specified through the type
parameter, but only affecting the given instance.
|
| Modifier and Type | Method and Description |
|---|---|
T |
getMockInstance()
Returns the mock instance exclusively associated with this mock-up instance.
|
void |
tearDown()
Discards the mock methods originally set up by instantiating this mock-up object, restoring mocked methods to
their original behaviors.
|
protected MockUp()
IllegalArgumentException - if no type to be mocked was specified;
or if multiple types were specified through a type variable but not all of them are interfaces;
or if there is a mock method for which no corresponding real method or constructor is found;
or if the real method matching a mock method is abstractMockUp(Class),
MockUp(Object)protected MockUp(Class<?> classToMock)
MockUp(),
MockUp(Object)protected MockUp(T instanceToMock)
getMockInstance() later gets called on this mock-up instance, it will return the instance that was
given here.instanceToMock - a real instance of the type to be mocked, meant to be the only one of that type that should
be affected by this mock-up instance; must not be nullMockUp(),
MockUp(Class)public final T getMockInstance()
MockUp(Object) constructor should have
been used instead.
In any case, for a given mock-up instance this method will always return the same mock instance.IllegalStateException - if called from a mock method for a static methodpublic final void tearDown()