For the latest stable version, please use Spring Framework 6.2.6!spring-doc.cadn.net.cn

Context Configuration with Component Classes

To load an ApplicationContext for your tests by using component classes (see Java-based container configuration), you can annotate your test class with @ContextConfiguration and configure the classes attribute with an array that contains references to component classes. The following example shows how to do so:spring-doc.cadn.net.cn

@ExtendWith(SpringExtension.class)
// ApplicationContext will be loaded from AppConfig and TestConfig
@ContextConfiguration(classes = {AppConfig.class, TestConfig.class}) (1)
class MyTest {
	// class body...
}
1 Specifying component classes.
@ExtendWith(SpringExtension::class)
// ApplicationContext will be loaded from AppConfig and TestConfig
@ContextConfiguration(classes = [AppConfig::class, TestConfig::class]) (1)
class MyTest {
	// class body...
}
1 Specifying component classes.
Component Classes

The term “component class” can refer to any of the following:spring-doc.cadn.net.cn

  • A class annotated with @Configuration.spring-doc.cadn.net.cn

  • A component (that is, a class annotated with @Component,@Service,@Repository, or other stereotype annotations).spring-doc.cadn.net.cn

  • A JSR-330 compliant class that is annotated with jakarta.inject annotations.spring-doc.cadn.net.cn

  • Any class that contains @Bean-methods.spring-doc.cadn.net.cn

  • Any other class that is intended to be registered as a Spring component (i.e., a Spring bean in the ApplicationContext), potentially taking advantage of automatic autowiring of a single constructor without the use of Spring annotations.spring-doc.cadn.net.cn

See the javadoc of @Configuration@Bean for further information regarding the configuration and semantics of component classes, paying special attention to the discussion of @Bean Lite Mode.spring-doc.cadn.net.cn

If you omit the classes attribute from the @ContextConfiguration annotation, the TestContext framework tries to detect the presence of default configuration classes. Specifically, AnnotationConfigContextLoaderAnnotationConfigWebContextLoader detect all static nested classes of the test class that meet the requirements for configuration class implementations, as specified in the @Configuration javadoc. Note that the name of the configuration class is arbitrary. In addition, a test class can contain more than one static nested configuration class if desired. In the following example, the OrderServiceTest class declares a static nested configuration class named Config that is automatically used to load the ApplicationContext for the test class:spring-doc.cadn.net.cn

@SpringJUnitConfig (1)
// ApplicationContext will be loaded from the static nested Config class
class OrderServiceTest {

	@Configuration
	static class Config {

		// this bean will be injected into the OrderServiceTest class
		@Bean
		OrderService orderService() {
			OrderService orderService = new OrderServiceImpl();
			// set properties, etc.
			return orderService;
		}
	}

	@Autowired
	OrderService orderService;

	@Test
	void testOrderService() {
		// test the orderService
	}

}
1 Loading configuration information from the nested Config类。
@SpringJUnitConfig (1)
// ApplicationContext will be loaded from the nested Config class
class OrderServiceTest {

	@Autowired
	lateinit var orderService: OrderService

	@Configuration
	class Config {

		// this bean will be injected into the OrderServiceTest class
		@Bean
		fun orderService(): OrderService {
			// set properties, etc.
			return OrderServiceImpl()
		}
	}

	@Test
	fun testOrderService() {
		// test the orderService
	}
}
1 Loading configuration information from the nested Config类。