The following annotations are supported with standard semantics for all Spring TestContext Framework configurations. Please note that these annotations are not specific to tests and can be used anywhere in the Spring Framework.

  • @Autowired
  • @Qualifier
  • @Value
  • @Resource (jakarta.annotation if JSR-250 is present
  • @ManagedBean (jakarta.annotation) if JSR-250 is present
  • @Inject (jakarta.inject) if JSR-330 is present
  • @Named (jakarta.inject) if JSR-330 is present
  • @PersistenceContext (javax.persistence) if JPA is present
  • @PersistenceUnit (javax.persistence) if JPA is present
  • @Transactional (org.springframework.transaction.annotation) with limited attribute support

JSR-250 Lifecycle Annotations

The Spring TestContext Framework can use the @PostConstruct and @PreDestroy annotations with standard semantics for any application components configured in the ApplicationContext. However, these lifecycle annotations have limited use in a real test class.

If a method in a test class is annotated with @PostConstruct, then that method runs before all before methods of the main test framework (for example, methods annotated with @BeforeEach from JUnit Jupiter), and this applies to every test method in the test class. On the other hand, if a method in a test class is marked with the @PreDestroy annotation, that method will never be executed. Therefore, within a test class, it is recommended to use test lifecycle callbacks from the main test environment instead of @PostConstruct and @PreDestroy annotations.