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