Context Configuration with Component Classes

如需通过使用组件类(请参阅 Java-based container configuration)加载 `ApplicationContext`用于您的测试,您可以使用 `@ContextConfiguration`注释您的测试类,并使用包含组件类引用的数组配置 `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:

Java
@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.
Kotlin
@ExtendWith(SpringExtension::class)
// ApplicationContext will be loaded from AppConfig and TestConfig
@ContextConfiguration(classes = [AppConfig::class, TestConfig::class]) (1)
class MyTest {
	// class body...
}
2 Specifying component classes.
Component Classes

术语“组件类”可以指以下任一项:

The term “component class” can refer to any of the following:

  • A class annotated with @Configuration.

  • A component (that is, a class annotated with @Component, @Service, @Repository, or other stereotype annotations).

  • A JSR-330 compliant class that is annotated with jakarta.inject annotations.

  • Any class that contains @Bean-methods.

  • 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.

有关组件类的配置和语义的更多信息,请参阅 javadoc @Configurationjavadoc @Bean,特别关注 @Bean 精简模式的讨论。

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

如果您从 @ContextConfiguration`注释中省略 `classes`属性,TestContext 框架将尝试检测默认配置类的存在。具体来说,`AnnotationConfigContextLoader`和 `AnnotationConfigWebContextLoader`检测符合配置类实现要求(如 javadoc `@Configuration 中指定)的测试类的所有 static`嵌套类。请注意,配置类的名称是任意的。此外,测试类可以包含多个 `static`嵌套配置类(如果需要)。在以下示例中,`OrderServiceTest`类声明了一个命名的 `Config`嵌套配置类 `static,该配置类自动用于为测试类加载 ApplicationContext

If you omit the classes attribute from the @ContextConfiguration annotation, the TestContext framework tries to detect the presence of default configuration classes. Specifically, AnnotationConfigContextLoader and AnnotationConfigWebContextLoader 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:

Java
@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 class.
Kotlin
@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
	}
}
2 Loading configuration information from the nested Config class.