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 that the value of a mock field or mock parameter will be an isolated {@linkplain Mocked mocked} instance,
17   * intended to be passed or <em>injected</em> into the code under test. Such instances can be said to be proper <em>mock
18   * objects</em>, in contrast to the mocked instances of a regular <code>@Mocked</code> type.
19   * <p>
20   * When the type of the injectable is <code>String</code>, a primitive wrapper, a {@linkplain Number number type}, or an
21   * enum, it is <em>not</em> mocked. A non-empty {@link #value} must then be provided, except in the first case where the
22   * empty string is used by default.
23   * <p>
24   * For the duration of each test where the mock field/parameter is in scope, <em>only one</em> injectable instance is
25   * mocked; other instances of the same mocked type are not affected. For an injectable mocked <em>class</em>, <em>static
26   * methods</em> and <em>constructors</em> are <em>not</em> mocked; only <em>non-native</em> instance methods are.
27   * <p>
28   * When used in combination with {@linkplain Tested @Tested}, the values of injectable fields and parameters will be
29   * used for automatic injection into the tested object. Additionally, this annotation can be applied to non-mocked
30   * fields of primitive or array types, which will also be used for injection.
31   *
32   * @see #value
33   * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#injectable" target="tutorial">Tutorial</a>
34   */
35  @Retention(RUNTIME)
36  @Target({ FIELD, PARAMETER })
37  public @interface Injectable {
38  
39      /**
40       * Specifies a literal value when the type of the injectable mock field/parameter is <code>String</code>, a
41       * primitive or wrapper type, a number type, or an enum type. For a primitive/wrapper/number type, the value
42       * provided must be convertible to it. For an enum type, the given textual value must equal the name of one of the
43       * possible enum values.
44       *
45       * @return the string
46       */
47      String value() default "";
48  }