@Retention(value=RUNTIME) @Target(value={FIELD,ANNOTATION_TYPE}) public @interface Tested
final
, then it is eligible for automatic instantiation and initialization.
By default, automatic creation occurs just before a test method is executed, provided the tested field remains
null
at such time; this default can be changed by specifying the availableDuringSetup()
optional
attribute as true
.
Whenever automatic creation occurs, a suitable instance of the tested class is created, initialized, and assigned to
the field.
Available injectables are used, either as argument values for the chosen constructor of the
tested class, or as values to set into injected fields of the newly-created tested object.
For constructor injection, all constructor parameters (if any) must be satisfied with the values of
available injectables.
If the tested class has a constructor annotated with the standard CDI annotation "@Inject
", then it is
the one to be used;
otherwise, if there are multiple satisfiable constructors then the one with the most parameters and the
widest accessibility (ie, first public
, then protected
, then package-private, and finally
private
) is chosen.
The matching between injectable values and constructor parameters is done by type when there is only one
parameter of a given type; otherwise, by type and name.
Whenever the tested object is created automatically, field injection is also performed.
Only non-final
fields are considered, between those declared in the tested class itself or in one of its
super-classes; at this time constructor injection already occurred, so only fields which remain uninitialized are
targeted.
For each such target field, the value of a still unused injectable of the same type is assigned, if
any is available.
Multiple target fields of the same type can be injected from separate injectables, provided each target field has the
same name as an available injectable of that type.
Finally, if there is no matching and available injectable value for a given target field, it is left unassigned,
unless the target field is for a required dependency; note that all fields marked with a DI annotation
(such as @Inject
, Autowired
, etc.) indicate required dependencies by default
(the use of "@Autowired(required = false)
" is respected, if present).
Whenever constructor or field injection is used, the value of each injectable goes into at most one matching
constructor parameter or instance field of a tested class.
The tested class can be abstract
.
In this case, if the tested field is left null then a subclass implementing all abstract methods is automatically
generated and instantiated.
The abstract method implementations are automatically mocked so that expectations can be recorded or
verified on them.
When the fullyInitialized()
attribute is true
, all eligible fields in the tested object will get
initialized with a suitable instance, which itself is recursively initialized in the same way.Modifier and Type | Optional Element and Description |
---|---|
boolean |
availableDuringSetup
Indicates whether the tested class gets instantiated and initialized before the execution of test setup
methods (ie, those annotated as
@Before or @BeforeMethod ), or after them. |
boolean |
fullyInitialized
Indicates that each and every field of the tested object should be assigned a value, either an
@Injectable or a real (unmocked) instance of the field type.
|
public abstract boolean availableDuringSetup
@Before
or @BeforeMethod
), or after them.
Typically, the early creation of tested objects is useful in a test setup method, which can use them for the
initialization of other objects.
Another potential use is to affect the initialization of other tested objects in the same test class, during their
creation after setup.
Finally, objects made available during setup are also available during the execution of any tear-down methods.
In order to be made available during test setup, a tested object won't be injected from @Injectable
test method parameters, only from injectable fields.public abstract boolean fullyInitialized
null
, an attempt is made to automatically
create and recursively initialize a suitable real instance.
For this attempt to succeed, the type of the field must either be a concrete class having a constructor that can
be satisfied by available injectables and/or by recursively created dependencies, an interface for which a single
implementation class is loaded, or a known interface (see below) for which a real instance can be created.
Currently, the JPA interfaces javax.persistence.EntityManagerFactory
and
javax.persistence.EntityManager
are supported, provided a META-INF/persistence.xml
file is
available in the runtime classpath.