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