Package org.springframework.test.context.support

# Class DependencyInjectionTestExecutionListener

java.lang.Object
org.springframework.test.context.support.AbstractTestExecutionListener
org.springframework.test.context.support.DependencyInjectionTestExecutionListener

All Implemented Interfaces:
  `org.springframework.core.Ordered`, `TestExecutionListener`

public class DependencyInjectionTestExecutionListener extends AbstractTestExecutionListener
`TestExecutionListener` which provides support for dependency injection and initialization of test instances.

Since:
  2.5
Author:
  Sam Brannen, Juergen Hoeller

## Field Summary

Fields
Modifier and Type
Field
Description
`static final int`
`ORDER`
The `order` value for this listener: 2000.
`static final String`
`REINJECT_DEPENDENCIES_ATTRIBUTE`
Attribute name for a `TestContext` attribute which indicates whether the dependencies of a test instance should be reinjected in `beforeTestMethod()`.

### Fields inherited from interface org.springframework.core.Ordered

`HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE`

## Constructor Summary

Constructors
Constructor
Description
`DependencyInjectionTestExecutionListener()`

## Method Summary

Modifier and Type
Method
Description
`void`
`beforeTestMethod(TestContext testContext)`
If the `REINJECT_DEPENDENCIES_ATTRIBUTE` in the supplied `test context` has a value of `Boolean.TRUE`, this method will have the same effect as `prepareTestInstance()`; otherwise, this method will have no effect.
`final int`
`getOrder()`
Returns 2000, which ensures that the `DependencyInjectionTestExecutionListener` is ordered after the `DirtiesContextBeforeModesTestExecutionListener` and the `BeanOverrideTestExecutionListener` and before the `MicrometerObservationRegistryTestExecutionListener` and the `DirtiesContextTestExecutionListener`.
`protected void`
`injectDependencies(TestContext testContext)`
Performs dependency injection and bean initialization for the supplied `TestContext` as described in `prepareTestInstance()`.
`void`
`prepareTestInstance(TestContext testContext)`
Performs dependency injection on the `test instance` of the supplied `test context` by `autowiring` and `initializing` the test instance via its own `application context` (without checking dependencies).

### Methods inherited from class org.springframework.test.context.support.AbstractTestExecutionListener

`afterTestClass, afterTestExecution, afterTestMethod, beforeTestClass, beforeTestExecution`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### ORDER

public static final int ORDER
The `order` value for this listener: 2000.

Since:
  6.2.3
See Also:
  - Constant Field Values

### REINJECT_DEPENDENCIES_ATTRIBUTE

public static final String REINJECT_DEPENDENCIES_ATTRIBUTE
Attribute name for a `TestContext` attribute which indicates whether the dependencies of a test instance should be reinjected in `beforeTestMethod()`. Note that dependencies will be injected in `prepareTestInstance()` in any case.

Clients of a `TestContext` (for example, other `TestExecutionListeners`) may therefore choose to set this attribute to signal that dependencies should be reinjected between execution of individual test methods.

Permissible values include `Boolean.TRUE` and `Boolean.FALSE`.

## Constructor Details

### DependencyInjectionTestExecutionListener

public DependencyInjectionTestExecutionListener()

## Method Details

### getOrder

public final int getOrder()
Returns 2000, which ensures that the `DependencyInjectionTestExecutionListener` is ordered after the `DirtiesContextBeforeModesTestExecutionListener` and the `BeanOverrideTestExecutionListener` and before the `MicrometerObservationRegistryTestExecutionListener` and the `DirtiesContextTestExecutionListener`.

Specified by:
  `getOrder` in interface `org.springframework.core.Ordered`
Overrides:
  `getOrder` in class `AbstractTestExecutionListener`

### prepareTestInstance

public void prepareTestInstance(TestContext testContext) throws Exception
Performs dependency injection on the `test instance` of the supplied `test context` by `autowiring` and `initializing` the test instance via its own `application context` (without checking dependencies).

The `REINJECT_DEPENDENCIES_ATTRIBUTE` will be subsequently removed from the test context, regardless of its value.

Specified by:
  `prepareTestInstance` in interface `TestExecutionListener`
Overrides:
  `prepareTestInstance` in class `AbstractTestExecutionListener`
Parameters:
  `testContext` - the test context for the test; never `null`
Throws:
  `Exception` - allows any exception to propagate

### beforeTestMethod

public void beforeTestMethod(TestContext testContext) throws Exception
If the `REINJECT_DEPENDENCIES_ATTRIBUTE` in the supplied `test context` has a value of `Boolean.TRUE`, this method will have the same effect as `prepareTestInstance()`; otherwise, this method will have no effect.

Specified by:
  `beforeTestMethod` in interface `TestExecutionListener`
Overrides:
  `beforeTestMethod` in class `AbstractTestExecutionListener`
Parameters:
  `testContext` - the test context in which the test method will be executed; never `null`
Throws:
  `Exception` - allows any exception to propagate
See Also:
  - `TestExecutionListener.afterTestMethod(org.springframework.test.context.TestContext)`
  - `TestExecutionListener.beforeTestExecution(org.springframework.test.context.TestContext)`
  - `TestExecutionListener.afterTestExecution(org.springframework.test.context.TestContext)`

### injectDependencies

protected void injectDependencies(TestContext testContext) throws Exception
Performs dependency injection and bean initialization for the supplied `TestContext` as described in `prepareTestInstance()`.

The `REINJECT_DEPENDENCIES_ATTRIBUTE` will be subsequently removed from the test context, regardless of its value.

Parameters:
  `testContext` - the test context for which dependency injection should be performed (never `null`)
Throws:
  `Exception` - allows any exception to propagate
See Also:
  - `prepareTestInstance(TestContext)`
  - `beforeTestMethod(TestContext)`