Package mockit

Annotation Type Mock


@Retention(RUNTIME) @Target(METHOD) public @interface Mock
Used inside a fake class to indicate a fake method whose implementation will temporarily replace the implementation of one or more matching "real" methods or constructors (these are the "faked" methods/constructors).

The fake method must have the same name and the same parameters in order to match a real method, except for an optional first parameter of type Invocation; if this extra parameter is present, the remaining ones (if any) must match the parameters in the real method. Alternatively, a single fake method having only the Invocation parameter will match all real methods of the same name, regardless of their parameters. The fake method must also have the same return type as the matching real method.

Method modifiers (public, final, static, etc.) between fake and faked methods don't have to be the same. It's perfectly fine to have a non-static fake method for a static faked method (or vice-versa), for example. Checked exceptions in the throws clause (if any) can also differ between the two matching methods.

A fake method can also target a constructor, in which case the previous considerations still apply, except for the name of the fake method which must be "$init".

Another special fake method, "void $clinit()", will target the static initializers of the faked class, if present in the fake class.

Yet another special fake method is "Object $advice(Invocation)", which if defined will match every method in the target class hierarchy.

See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    int
    Number of expected invocations of the mock method.
    int
    Maximum number of expected invocations of the mock method, if positive.
    int
    Minimum number of expected invocations of the mock method, starting from 0 (zero, which is the default).
  • Element Details

    • invocations

      int invocations
      Number of expected invocations of the mock method. If 0 (zero), no invocations will be expected. A negative value (the default) means there is no expectation on the number of invocations; that is, the mock can be called any number of times or not at all during any test which uses it.

      A non-negative value is equivalent to setting minInvocations and maxInvocations to that same value.

      Returns:
      Number of expected invocations of the mock method
      Default:
      -1
    • minInvocations

      int minInvocations
      Minimum number of expected invocations of the mock method, starting from 0 (zero, which is the default).
      Returns:
      Minimum number of expected invocations of the mock method
      See Also:
      Default:
      0
    • maxInvocations

      int maxInvocations
      Maximum number of expected invocations of the mock method, if positive. If zero the mock is not expected to be called at all. A negative value (the default) means there is no expectation on the maximum number of invocations.
      Returns:
      Maximum number of expected invocations of the mock method
      See Also:
      Default:
      -1