View Javadoc
1   /*
2    * Copyright (c) 2006 JMockit developers
3    * This file is subject to the terms of the MIT license (see LICENSE.txt).
4    */
5   package mockit;
6   
7   import static java.lang.annotation.ElementType.FIELD;
8   import static java.lang.annotation.ElementType.PARAMETER;
9   import static java.lang.annotation.RetentionPolicy.RUNTIME;
10  
11  import java.lang.annotation.Retention;
12  import java.lang.annotation.Target;
13  
14  /**
15   * Indicates an instance field of a test class as being a <em>mock field</em>, or a parameter of a test method as a
16   * <em>mock parameter</em>; in either case, the declared type of the field/parameter is a <em>mocked type</em>, whose
17   * instances are <em>mocked instances</em>.
18   * <p>
19   * Mocked types can also be introduced by other annotations: {@linkplain Injectable @Injectable},
20   * {@link Capturing @Capturing}. Their effect is to <em>constrain</em> or <em>extend</em> the mocking capabilities here
21   * specified.
22   * <p>
23   * Any type can be mocked, except for primitive and array types. A mocked instance of that type is automatically created
24   * and assigned to the mock field/parameter, for use when {@linkplain Expectations recording} and/or
25   * {@linkplain Verifications verifying} expectations. For a mock <em>field</em>, the test itself can provide the
26   * instance by declaring the field as <code>final</code> and assigning it the desired instance (or <code>null</code>).
27   * <p>
28   * The effect of declaring a <code>@Mocked</code> type, <em>by default</em>, is that all new instances of that type, as
29   * well as those previously created, will also be mocked instances; this will last for the duration of each test where
30   * the associated mock field/parameter is in scope. All non-<code>private</code> methods of the mocked type will be
31   * mocked.
32   * <p>
33   * When the mocked type is a class, all super-classes up to but not including <code>java.lang.Object</code> are also
34   * mocked. Additionally, <em>static methods</em> and <em>constructors</em> are mocked as well, just like instance
35   * methods; <em>native</em> methods are also mocked, provided they are <code>public</code> or <code>protected</code>.
36   * <p>
37   * While a method or constructor is mocked, an invocation does not result in the execution of the original code, but in
38   * a (generated) call into JMockit, which then responds with either a default or a <em>recorded</em>
39   * {@linkplain Expectations#result result} (or with a {@linkplain Expectations#times constraint} violation, if the
40   * invocation is deemed to be unexpected).
41   * <p>
42   * Mocking will automatically <em>cascade</em> into the return types of all non-<code>void</code> methods belonging to
43   * the mocked type, except for non-eligible ones (primitive wrappers, <code>String</code>, and collections/maps). When
44   * needed, such cascaded returns can be overridden by explicitly recording a return value for the mocked method. If
45   * there is a mock field/parameter with the same type (or a subtype) of some cascaded type, then the original instance
46   * from that mock field/parameter will be used as the cascaded instance, rather than a new one being created; this
47   * applies to all cascading levels, and even to the type of the mock field/parameter itself (ie, if a method in
48   * class/interface "<code>A</code>" has return type <code>A</code>, then it will return itself by default). Finally,
49   * when new cascaded instances are created, {@linkplain Injectable @Injectable} semantics apply.
50   * <p>
51   * Static <em>class initializers</em> (including assignments to <em>static</em> fields) of a mocked class are not
52   * affected, unless {@linkplain #stubOutClassInitialization specified otherwise}.
53   *
54   * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#mocked" target="tutorial">Tutorial</a>
55   */
56  @Retention(RUNTIME)
57  @Target({ FIELD, PARAMETER })
58  public @interface Mocked {
59  
60      /**
61       * Indicates whether <em>static initialization code</em> in the mocked class should be stubbed out or not. Static
62       * initialization includes the execution of assignments to static fields of the class and the execution of static
63       * initialization blocks, if any. (Note that <em>static final</em> fields initialized with <em>compile-time</em>
64       * constants are not assigned at runtime, remaining unaffected whether the class is stubbed out or not.)
65       * <p>
66       * By default, static initialization code in a mocked class is <em>not</em> stubbed out. The JVM will only perform
67       * static initialization of a class <em>once</em>, so stubbing out the initialization code can have unexpected
68       * consequences. Stubbing out the static initialization of a class is an unsafe operation, which can cause other
69       * tests, executed later in the same test run, to unexpectedly fail; instead of resorting to stubbing out a class's
70       * static initializer, the root cause for wanting to stub it out should be eliminated. Caveat Emptor.
71       *
72       * @return true, if successful
73       */
74      boolean stubOutClassInitialization() default false;
75  }