Spring TestContext Framework

Spring TestContext Framework(位于 org.springframework.test.context 包中)提供通用的、基于注释的单元和集成测试支持,它与所使用的测试框架无关。TestContext 框架还非常重视惯例胜于配置,并带有合理的默认值,你可以通过基于注释的配置将其覆盖。

The Spring TestContext Framework (located in the org.springframework.test.context package) provides generic, annotation-driven unit and integration testing support that is agnostic of the testing framework in use. The TestContext framework also places a great deal of importance on convention over configuration, with reasonable defaults that you can override through annotation-based configuration.

除了统测试基础架构之外,TestContext 框架还对 JUnit 4、JUnit Jupiter(即 JUnit 5)和 TestNG 提供显式支持。对于 JUnit 4 和 TestNG,Spring 提供 abstract 支持类。此外,Spring 还提供针对 JUnit 4 的自定义 JUnit Runner 和自定义 JUnit Rules,以及针对 JUnit Jupiter 的自定义 Extension,让你可以编写所谓的 POJO 测试类。POJO 测试类不需要扩展任何特定的类层次结构,例如 abstract 支持类。

In addition to generic testing infrastructure, the TestContext framework provides explicit support for JUnit 4, JUnit Jupiter (AKA JUnit 5), and TestNG. For JUnit 4 and TestNG, Spring provides abstract support classes. Furthermore, Spring provides a custom JUnit Runner and custom JUnit Rules for JUnit 4 and a custom Extension for JUnit Jupiter that let you write so-called POJO test classes. POJO test classes are not required to extend a particular class hierarchy, such as the abstract support classes.

以下部分概述了 TestContext 框架的内部。如果您仅对使用框架感兴趣,而对使用您自己的自定义侦听器或自定义加载器对其进行扩展不感兴趣,请随时直接转到配置( context managementdependency injectiontransaction management )、 support classesannotation support 部分。

The following section provides an overview of the internals of the TestContext framework. If you are interested only in using the framework and are not interested in extending it with your own custom listeners or custom loaders, feel free to go directly to the configuration (context management, dependency injection, transaction management ), support classes, and annotation support sections.