Package mockit

Class MockUp<T>

java.lang.Object
mockit.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 abstract class MockUp<T> extends Object
A base class used in the creation of a fake for an external type, which is usually a class from some library or component used from the internal codebase of the system under test (SUT). Such fake classes can be used as fake implementations for use in unit or integration tests. For example:

 public final class FakeSystem <strong>extends MockUp&lt;System></strong> {
    <strong>&#64;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:


 &#64;Test
 public &lt;<strong>BC extends SomeBaseClass</strong>> void someTest() {
     new MockUp&lt;<strong>BC</strong>>() {
        &#64;Mock int someMethod(int i) { return i + 1; }
     };

     int i = new AConcreteSubclass().someMethod(1);
     assertEquals(2, i);
 }
 
See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final Type
    Holds the class or generic type targeted by this fake instance.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Applies the fake 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.
  • Method Summary

    Modifier and Type
    Method
    Description
    final 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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • targetType

      protected final Type 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

      protected MockUp(Class<?> targetClass)
      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

      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.

      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

      public final T 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 the 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.

      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.