T - specifies the type (class, interface, etc.) to be faked; multiple interfaces can be faked 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 and it extends a single type, then all implementation classes extending or
implementing that base type are also faked;
if the type argument itself is a parameterized type, then only its raw type is consideredpublic abstract class MockUp<T> extends Object
public final class FakeSystem extends MockUp<System> {
@Mock public static long nanoTime() { return 123L; }
}
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 faked class/interface.
At runtime, the execution of a faked method/constructor will get redirected to the corresponding mock method.
When the faked type is 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.
In this case, an instance of the generated implementation class should be obtained by calling
getMockInstance().
When the type to be faked is specified indirectly through a type variable, there are two
other possible outcomes:
extends" two or more interfaces, a mock 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());
}
final class), then
that type is taken as a base type whose concrete implementation classes should also get faked.
Example:
@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(),
onTearDown(),
targetType,
Tutorial| Modifier and Type | Field and Description |
|---|---|
protected Type |
targetType
Holds the class or generic type targeted by this mock-up instance.
|
| 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<?> targetClass)
Applies the mock methods defined in the mock-up subclass to the given class/interface.
|
protected |
MockUp(T targetInstance)
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.
|
protected void |
onTearDown()
An empty method that can be overridden in a mock-up subclass that wants to be notified whenever the mock-up is
automatically torn down.
|
protected final Type targetType
protected MockUp()
IllegalArgumentException - if no type to be faked was specified;
or if multiple types were specified through a type variable but not all of them are interfaces;
or there is a mock method for which no corresponding real method or constructor is found;
or the real method matching a mock method is abstract;
or if an unbounded type variable was used as the base type to be fakedMockUp(Class),
MockUp(Object)protected MockUp(Class<?> targetClass)
MockUp(),
MockUp(Object)protected MockUp(T targetInstance)
getMockInstance() later gets called on this mock-up instance, it will return the instance that was
given here.targetInstance - a real instance of the type to be faked, 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 method, or if called on a mock-up whose
base type was specified by a type variableprotected void onTearDown()
mockups" system property.
By default, this method does nothing.Copyright © 2006–2016. All rights reserved.