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.ANNOTATION_TYPE;
8   import static java.lang.annotation.ElementType.FIELD;
9   import static java.lang.annotation.ElementType.METHOD;
10  import static java.lang.annotation.ElementType.PARAMETER;
11  import static java.lang.annotation.RetentionPolicy.RUNTIME;
12  
13  import java.lang.annotation.ElementType;
14  import java.lang.annotation.Retention;
15  import java.lang.annotation.RetentionPolicy;
16  import java.lang.annotation.Target;
17  
18  /**
19   * Indicates a class to be tested, with optional automatic instantiation and/or automatic injection of dependencies.
20   * This annotation is applicable to instance fields of a test class, and to test method parameters; alternatively, it
21   * can be used as a meta-annotation on a user-defined annotation which, in turn, needs to have
22   * {@linkplain RetentionPolicy#RUNTIME runtime} {@linkplain Retention retention} and be {@linkplain Target applicable}
23   * to {@linkplain ElementType#FIELD fields} and/or {@linkplain ElementType#PARAMETER parameters}.
24   * <p>
25   * Every non-<code>final</code> tested field and every tested parameter is eligible for automatic instantiation and
26   * initialization. By default, automatic instantiation occurs just before a test method is executed. This default can be
27   * changed by specifying the {@link #availableDuringSetup} optional attribute as <code>true</code> in a tested field
28   * declaration (it is ignored if applied to a tested parameter).
29   * <p>
30   * Whenever automatic creation occurs, a suitable instance of the tested class is created, initialized, and assigned to
31   * the tested field or passed as argument to the tested parameter. Available {@linkplain Injectable injectables} and
32   * other <code>@Tested</code> values are used, either as argument values for the chosen constructor of the tested class,
33   * or as values to set into injected fields of the newly-created tested object. For a given tested object, only
34   * <em>preceding</em> tested objects (if any) are regarded as available for injection; other such objects declared after
35   * the one being created are disregarded. <code>@Tested</code> parameters precede any <code>@Tested</code> fields.
36   * <p>
37   * For <em>constructor injection</em>, all constructor parameters (if any) must be satisfied with available
38   * tested/injectable values. If the tested class has a constructor annotated with the standard CDI annotation
39   * "<code>@Inject</code>", then it is the one to be used; otherwise, if there are multiple satisfiable constructors then
40   * the one with the most parameters <em>and</em> the widest accessibility (ie, first <code>public</code>, then
41   * <code>protected</code>, then <em>package-private</em>, and finally <code>private</code>) is chosen. The matching
42   * between injectable values and constructor parameters is done by <em>type</em> when there is only one parameter of a
43   * given type; otherwise, by type <em>and name</em>.
44   * <p>
45   * <em>Field injection</em> is performed on all tested objects, even when it was not instantiated automatically. Only
46   * non-<code>final</code> fields are considered, between those declared in the tested class itself or in one of its
47   * super-classes; at this time constructor injection already occurred, so only fields which remain uninitialized are
48   * targeted. For each such <em>target</em> field, the value of a still unused injectable of a matching type is assigned,
49   * if any is available. When a tested object has multiple target fields of a matching type, not just the type but also
50   * the <em>name</em> of each field will be used when looking for available injectables. Finally, if there is no matching
51   * and available injectable value for a given target field, it is left unassigned, unless the target field is for a
52   * <em>required</em> dependency; note that all fields marked with a DI annotation (such as <code>@Inject</code>,
53   * <code>@Autowired</code>, etc.) indicate required dependencies by default (the use of
54   * "<code>@Autowired(required = false)</code>" is respected, if present).
55   * <p>
56   * Tested fields/parameters whose declared type is primitive, a primitive wrapper, numeric, or an enum can use the
57   * {@link #value} attribute to specify an initial value from a string.
58   * <p>
59   * Custom names specified in field annotations from Java EE (<code>@Resource(name)</code>, <code>@Named</code>) or the
60   * Spring framework (<code>@Qualifier</code>) are used when looking for a matching <code>@Injectable</code> or
61   * <code>@Tested</code> value. When such a name contains a <code>-</code> (dash) or <code>.</code> (dot) character, the
62   * corresponding camel-cased name is used instead.
63   * <p>
64   * Whenever constructor or field injection is used, the value of each injectable goes into <em>at most one</em> matching
65   * constructor parameter or instance field of a tested class.
66   * <p>
67   * The tested class can be <code>abstract</code>. In this case, if the tested field is left null then a subclass
68   * implementing all abstract methods is automatically generated and instantiated. The abstract method implementations
69   * are automatically <em>mocked</em> so that expectations can be recorded or verified on them.
70   * <p>
71   * When the {@link #fullyInitialized} attribute is <code>true</code>, all eligible fields in the tested object will get
72   * initialized with a suitable instance, which itself is recursively initialized in the same way.
73   * <p>
74   * Finally, if the {@link #global} attribute is <code>true</code>, then a single <em>global</em> instance will be
75   * created during the test run, for the name of the annotated field/parameter. This is useful for the creation of
76   * scenario-oriented tests, where each test in a test class exercises a step in the scenario, with all of them accessing
77   * the same state in one (or more) global tested objects.
78   *
79   * @see <a href="http://jmockit.github.io/tutorial/Mocking.html#tested" target="tutorial">Tutorial</a>
80   */
81  @Retention(RUNTIME)
82  @Target({ FIELD, PARAMETER, ANNOTATION_TYPE, METHOD })
83  public @interface Tested {
84  
85      /**
86       * Specifies a literal value when the type of the tested field/parameter is <code>String</code>, a primitive or
87       * wrapper type, a number type, or an enum type. For a primitive/wrapper/number type, the value provided must be
88       * convertible to it. For an enum type, the given textual value must equal the name of one of the possible enum
89       * values.
90       *
91       * @return the string
92       */
93      String value() default "";
94  
95      /**
96       * Indicates that each non-<code>final</code> field of the tested object that is eligible for injection should be
97       * assigned a value, which can be an available {@linkplain Injectable @Injectable} or <code>@Tested</code> value of
98       * a type assignable to the field type, or a real (unmocked) instance of the field type.
99       * <p>
100      * Non-eligible fields are those that have already being assigned from a constructor, or that have a primitive,
101      * array, annotation, or JRE type (with the exception of the types described below, which are given special
102      * treatment). Also non-eligible are any <code>static</code> or <code>volatile</code> fields, unless annotated with
103      * an <em>injection annotation</em> (one of <code>@Inject</code>, <code>@Resource</code>, <code>@Autowired</code>,
104      * <code>@EJB</code>, <code>@PersistenceContext</code>, or <code>@PersistenceUnit</code>).
105      * <p>
106      * For each field of a reference type that would otherwise remain <code>null</code>, an attempt is made to
107      * automatically create and recursively initialize a suitable real instance. For this attempt to succeed, the type
108      * of the field must either be a concrete class having a constructor that can be satisfied by available
109      * tested/injectable values and/or by recursively created dependencies, or a known interface (see below) for which a
110      * real instance can be created.
111      * <p>
112      * Constructor injection is also supported. In this case, the same rules used for injected fields apply to the
113      * parameters of the constructor that gets chosen for automatic instantiation.
114      * <p>
115      * Currently, the following standard types (some of which are Java EE interfaces) have special support:
116      * <ul>
117      * <li>{@link java.util.logging.Logger}: a logger is automatically
118      * {@linkplain java.util.logging.Logger#getLogger(String) created} with the name of the tested class.</li>
119      * <li>{@link javax.sql.DataSource}: a JDBC data source is created and configured according to a matching
120      * {@link javax.annotation.sql.DataSourceDefinition @DataSourceDefinition} in the tested class.</li>
121      * <li>{@link javax.inject.Provider Provider&lt;T>}: a provider which produces an instance of type <code>T</code> is
122      * injected.</li>
123      * <li>JPA interfaces {@link javax.persistence.EntityManagerFactory} and {@link javax.persistence.EntityManager}:
124      * created through calls to {@link javax.persistence.Persistence#createEntityManagerFactory(String)} and
125      * {@link javax.persistence.EntityManagerFactory#createEntityManager()}, provided a suitable
126      * <code>META-INF/persistence.xml</code> file is available in the runtime classpath.</li>
127      * <li>Servlet interfaces {@link javax.servlet.ServletContext} and {@link javax.servlet.http.HttpSession}: objects
128      * that emulate the servlet context and HTTP session are automatically created for use in tests.</li>
129      * <li>{@link javax.enterprise.context.Conversation}: an object that emulates a web application's conversation
130      * context is created.</li>
131      * </ul>
132      *
133      * @return true, if successful
134      */
135     boolean fullyInitialized() default false;
136 
137     /**
138      * Indicates whether the tested class gets instantiated and initialized <em>before</em> the execution of test setup
139      * methods (ie, those annotated as <code>@Before</code> or <code>@BeforeMethod</code>), or <em>after</em> them.
140      * <p>
141      * Typically, the early creation of tested objects is useful in a test setup method, which can use them for the
142      * initialization of other objects. Another potential use is to affect the initialization of other tested objects in
143      * the same test class, during their creation <em>after</em> setup. Finally, objects made available during setup are
144      * also available during the execution of any tear-down methods.
145      *
146      * @return true, if successful
147      */
148     boolean availableDuringSetup() default false;
149 
150     /**
151      * Indicates whether a single <em>named</em> instance of the tested class is to be created and used for the whole
152      * test run. The name is the same as the annotated field or test method parameter.
153      *
154      * @return true, if successful
155      */
156     boolean global() default false;
157 }