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