Class MockUp<T>
- Type Parameters:
T
- specifies the type to be faked; if a type variable is used, 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 considered
- Direct Known Subclasses:
FakeBeanFactory
,FakeFrameworkMethod
,FakeRunNotifier
public final class FakeSystem <strong>extends MockUp<System></strong> {
<strong>@Mock</strong> public static long nanoTime() { return 123L; }
}
One or more fake 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. At runtime, the execution
of a faked method/constructor will get redirected to the corresponding fake method.
When the type to be faked is specified indirectly through a type variable, then that type is taken as a base type whose concrete implementation classes should also get faked. Example:
@Test
public <<strong>BC extends SomeBaseClass</strong>> void someTest() {
new MockUp<<strong>BC</strong>>() {
@Mock int someMethod(int i) { return i + 1; }
};
int i = new AConcreteSubclass().someMethod(1);
assertEquals(2, i);
}
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Type
Holds the class or generic type targeted by this fake instance. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
MockUp()
Applies the fake methods defined in the concrete subclass to the class or interface specified through the type parameter.protected
Applies the mock methods defined in the mock-up subclass to the given class/interface.protected
Applies the mock methods defined in the mock-up subclass to the type specified through the type parameter, but only affecting the given instance. -
Method Summary
Modifier and TypeMethodDescriptionfinal T
Returns the mock instance exclusively associated with this mock-up instance.protected void
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.
-
Field Details
-
targetType
Holds the class or generic type targeted by this fake instance.
-
-
Constructor Details
-
MockUp
protected MockUp()Applies the fake methods defined in the concrete subclass to the class or interface specified through the type parameter.- See Also:
-
MockUp
Applies the mock methods defined in the mock-up subclass to the given class/interface.In most cases, the constructor with no parameters can be used. This variation should be used only when the type to be faked is not accessible or known from the test code.
- Parameters:
targetClass
- the target class- See Also:
-
MockUp
Applies the mock methods defined in the mock-up subclass to the type specified through the type parameter, but only affecting the given instance.In most cases, the constructor with no parameters should be adequate. This variation can be used when mock data or behavior is desired only for a particular instance, with other instances remaining unaffected; or when multiple mock-up objects carrying different states are desired, with one mock-up instance per real instance.
If
getMockInstance()
later gets called on this mock-up instance, it will return the instance that was given here.- Parameters:
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- See Also:
-
-
Method Details
-
getMockInstance
Returns the mock instance exclusively associated with this mock-up instance. If the mocked type was an interface, then said instance is the one that was automatically created when the mock-up was applied. If it was a class, and no such instance is currently associated with this (stateful) mock-up object, then a new uninitialized instance of the faked class is created and returned, becoming associated with the mock-up. If a regular initialized instance was desired, then theMockUp(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.
- Returns:
- the mock instance
- See Also:
-
onTearDown
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. Tear down happens when the mock-up goes out of scope: at the end of the test when applied inside a test, at the end of the test class when applied before the test class, or at the end of the test run when applied through the "mockups
" system property.By default, this method does nothing.
-