# JUnit

**JUnit** 是 Java 语言的单元测试框架，由 [Kent Beck](https://www.edgepedia.org/zh/kent-beck) 和 Erich Gamma 于 1997 年创建，在一项涵盖 630 万个公开 GitHub Java 项目的调查中，它是最广泛使用的 Java 测试框架，被 48% 的项目导入。<sup>[1](https://www.mdpi.com/2076-3417/11/16/7250)</sup><sup> • </sup><sup>[2](https://dirkriehle.com/wp-content/uploads/2008/04/sen-2008-03-junit-doc.pdf)</sup> 当前一代 JUnit 6 要求 Java 17 和 Kotlin 2.1 或以上版本，为 JVM 上的开发者侧测试提供了基础。<sup>[3](https://junit.org/)</sup> 自版本 5 起，该框架由三个子项目组成：JUnit Platform、JUnit Jupiter 和 JUnit Vintage。<sup>[4](https://docs.junit.org/5.11.0/user-guide/)</sup>

| 关键事实 | 详情 |
|---|---|
| 起源 | 由 Kent Beck 和 Erich Gamma 在从苏黎世飞往 1997 年亚特兰大 OOPSLA 会议的航班上结对编程写成<sup>[5](https://martinfowler.com/bliki/Xunit.html)</sup> |
| 架构（自 5 起） | JUnit Platform（启动器与 TestEngine API）+ JUnit Jupiter（编程模型与扩展模型）+ JUnit Vintage（运行 JUnit 3/4 测试）<sup>[4](https://docs.junit.org/5.11.0/user-guide/)</sup> |
| 采用率 | 在 630 万个公开 GitHub Java 项目中被 48% 导入；TestNG 占 3%<sup>[1](https://www.mdpi.com/2076-3417/11/16/7250)</sup> |
| 当前一代 | JUnit 6，要求 Java 17 和 Kotlin 2.1 或以上版本<sup>[3](https://junit.org/)</sup> |
| 工具支持 | 在 IntelliJ IDEA、Eclipse、NetBeans、Visual Studio Code、Gradle、Maven 和 Ant 中提供一流的 Platform 支持<sup>[4](https://docs.junit.org/5.11.0/user-guide/)</sup> |
| 2025 年发布 | 5.12（2025 年 2 月 21 日）、5.13（5 月发布，支持参数化测试类）以及 6.0（当时计划于 9 月发布）<sup>[6](https://docs.junit.org/5.12.0/release-notes/)</sup><sup> • </sup><sup>[7](https://speakerdeck.com/marcphilipp/catching-up-with-junit)</sup> |
| 不稳定测试证据 | 在研究的 19,532 个 JUnit 测试方法中，五种测试坏味道在 75% 的案例中与不稳定测试存在因果共现，重构可修复这些案例<sup>[8](https://dl.acm.org/doi/abs/10.1007/s10664-019-09683-z)</sup> |

## 历史与 xUnit 谱系

JUnit 诞生于从苏黎世飞往 1997 年亚特兰大 OOPSLA 会议的航班上，Kent Beck 和 Erich Gamma 在那里以测试先行的方式结对编程写出了第一个版本。<sup>[5](https://martinfowler.com/bliki/Xunit.html)</sup> 其设计源自 Beck 的 Smalltalk 测试框架 SUnit。<sup>[2](https://dirkriehle.com/wp-content/uploads/2008/04/sen-2008-03-junit-doc.pdf)</sup> Beck 普及了单元测试和测试驱动开发，是《敏捷宣言》的最初签署人之一，也是 JUnit 最初的合著者。<sup>[9](https://blogs.oracle.com/javamagazine/post/interview-with-kent-beck)</sup>

该框架迅速流行起来，对支持日益壮大的极限编程和测试驱动开发运动至关重要。<sup>[5](https://martinfowler.com/bliki/Xunit.html)</sup> 它的名字成为跨语言测试框架的家族姓氏：xUnit 家族源自 JUnit——其中第一个广为人知的框架——而 Michael Feathers 为 C++ 编写的 CppUnit 可能是第一个移植版本，此后几乎每种语言都至少有一个移植版本。<sup>[5](https://martinfowler.com/bliki/Xunit.html)</sup> JUnit 还启发了 .NET 的 NUnit。<sup>[10](https://formal.kastel.kit.edu/teaching/pse/201011/junit.pdf)</sup>

JUnit 3.8 是“旧”设计的最后一个主要版本，之后在 4.0 版本中转向 Java 注解。<sup>[2](https://dirkriehle.com/wp-content/uploads/2008/04/sen-2008-03-junit-doc.pdf)</sup> 在 JUnit 3.x 中，测试方法通过以“test”开头的命名约定来识别，TestCase 和 TestSuite 类实现了 Test 接口。<sup>[2](https://dirkriehle.com/wp-content/uploads/2008/04/sen-2008-03-junit-doc.pdf)</sup> 大约在 2003 年，Java 5 的注解促使 Cédric Beust 创建了 [TestNG](https://www.edgepedia.org/zh/testng)，它用注解取代了 JUnit 的命名约定和继承；JUnit 4 在很大程度上模仿 TestNG 而来，旧的 JUnit 则被重新标记为 JUnit 3。<sup>[11](https://www.accu.org/journals/overload/24/131/winder_2203/)</sup> JUnit 4 是一次完全重新开发，使用了 Java 1.5 的特性，例如 @Test 注解。<sup>[10](https://formal.kastel.kit.edu/teaching/pse/201011/junit.pdf)</sup>

## 架构：Platform、Jupiter 与 Vintage

JUnit 5 的决定性变化是架构上的。JUnit Platform 是在 JVM 上启动测试框架的基础，并定义了用于开发在该平台上运行的测试框架的 TestEngine API。<sup>[4](https://docs.junit.org/5.11.0/user-guide/)</sup> 在 JUnit 5 中，用于编写测试和扩展的编程模型与扩展模型被整合到 JUnit Jupiter 中，后者还提供了一个 TestEngine，使基于 Jupiter 的测试能够在平台上运行。<sup>[4](https://docs.junit.org/5.11.0/user-guide/)</sup> 为了在平台上运行针对 JUnit 3 或 JUnit 4 编写的测试，JUnit Vintage 提供了一个 TestEngine，且类路径或模块路径上必须存在 4.12 或更新版本的 JUnit。<sup>[4](https://docs.junit.org/5.11.0/user-guide/)</sup>

正是这种分层让其他测试引擎能够在同一基础设施上运行：任何实现 TestEngine API 的框架都可以在 Platform 上启动。Platform 还附带一个用于命令行启动的 Console Launcher，以及用于用一个或多个测试引擎运行自定义测试套件的 JUnit Platform Suite Engine。<sup>[4](https://docs.junit.org/5.11.0/user-guide/)</sup> 例如，JUnit 5.14.4 捆绑了 Platform 1.14.4、Jupiter 5.14.4 和 Vintage 5.14.4。<sup>[12](https://github.com/junit-team/junit-framework/releases)</sup>

## 编写与运行测试

Jupiter 测试是标注了 @Test 的方法。与 JUnit 4 的 @Test 不同，Jupiter 的 @Test 不声明任何属性，因为 Jupiter 扩展使用自己的专用注解。<sup>[4](https://docs.junit.org/5.11.0/user-guide/)</sup> 生命周期注解 @BeforeEach、@AfterEach、@BeforeAll 和 @AfterAll 类似于 JUnit 4 的 @Before、@After、@BeforeClass 和 @AfterClass。<sup>[4](https://docs.junit.org/5.11.0/user-guide/)</sup> 所有 Jupiter 断言都是 org.junit.jupiter.api.Assertions 类中的静态方法，包括适合 Java 8 lambda 的新增方法。<sup>[4](https://docs.junit.org/5.11.0/user-guide/)</sup>

**参数化测试与动态测试。** 参数化测试用不同参数多次运行同一个测试；它们的声明方式与常规 @Test 方法类似，但使用 @ParameterizedTest 并至少提供一个参数源。<sup>[4](https://docs.junit.org/5.11.0/user-guide/)</sup> 动态测试由 @TestFactory 方法在运行时生成，这些方法本身不是测试用例，而是测试用例的工厂，返回 Stream、Collection、Iterable、Iterator 或 DynamicNode 数组。<sup>[13](https://junit.org/junit5/docs/5.3.1/user-guide/index.html)</sup> Jupiter 还支持 @RepeatedTest、@TestTemplate、@TestClassOrder、@TestMethodOrder、@TestInstance、@DisplayName 和 @DisplayNameGeneration。<sup>[4](https://docs.junit.org/5.11.0/user-guide/)</sup>

历史上，结果是通过一个从测试传递到测试的收集参数对象来收集的；TestResult 是框架的一个扩展点，默认实现统计失败和错误，客户端可以定义自定义实现，例如 HTMLTestResult。<sup>[14](https://curlunit.sourceforge.net/doc/cookstour/cookstour.htm)</sup>

## 与构建工具、IDE 和 CI 的集成

[IntelliJ IDEA](https://www.edgepedia.org/zh/intellij-idea)、Eclipse、NetBeans 和 [Visual Studio Code](https://www.edgepedia.org/zh/visual-studio-code)，以及构建工具 Gradle、Maven 和 Ant 中，都提供对 JUnit Platform 的一流支持。<sup>[4](https://docs.junit.org/5.11.0/user-guide/)</sup> Gradle 的 Test 任务使用任何受支持的测试库——JUnit、JUnit Platform 或 TestNG——运行测试用例，并生成“JUnit XML”伪标准格式的 XML 结果，该格式被 JUnit 4、JUnit Jupiter 和 TestNG 使用，并由 CI 服务器消费。<sup>[15](https://docs.gradle.org/current/userguide/java_testing.html)</sup>

## 数据一览

一项针对 630 万个公开 GitHub Java 项目的研究发现，52% 的项目至少包含一个测试用例，JUnit 是最常用的 Java 测试框架，占 48% 的份额，而 TestNG 占 3%。<sup>[1](https://www.mdpi.com/2076-3417/11/16/7250)</sup> 在使用 JUnit 的项目中，2.84% 仅使用 JUnit 3，35.53% 同时使用 JUnit 4 和 JUnit 5，9.59% 组合使用 JUnit 3、4 和 5。<sup>[1](https://www.mdpi.com/2076-3417/11/16/7250)</sup>

JUnit 5 的具体采用率因所研究的群体而异。一项对 GitHub 上 485 个使用 JUnit 的流行 Java 开源项目的分析发现，只有 15.9% 使用 JUnit 5 库。<sup>[16](https://ieeexplore.ieee.org/document/9769994)</sup> 在 JUnit 5 采用者中，功能使用集中：在检测到的 17 个 JUnit 5 功能中，只有 3 个（17.6%）占使用量的 70% 以上。<sup>[16](https://ieeexplore.ieee.org/document/9769994)</sup>

## 与 TestNG 及 xUnit 家族的比较

JUnit 和 TestNG 有重叠，但目标范围不同。TestNG 将自己定位为覆盖广泛的测试需求，从单个类的单元测试到涉及应用服务器等外部框架的整个系统的集成测试。<sup>[17](https://testng.org/documentation.html)</sup> TestNG 还允许通过 testng.xml 显式控制测试执行顺序，顺序保留在 TestNG 5.14.5 中引入。<sup>[15](https://docs.gradle.org/current/userguide/java_testing.html)</sup> 相比之下，JUnit 将并行性视为可选功能（见下文）。历史上影响是双向的：TestNG 的注解模型塑造了 JUnit 4，而 JUnit 的名字和设计孕育了跨语言的 xUnit 家族。<sup>[11](https://www.accu.org/journals/overload/24/131/winder_2203/)</sup><sup> • </sup><sup>[5](https://martinfowler.com/bliki/Xunit.html)</sup>

## 测试质量与常见陷阱

**不稳定测试。** 一项针对 18 个软件系统中 19,532 个 JUnit 测试方法的大规模实证研究发现，五种诱发不稳定性的测试坏味道（资源乐观、间接测试、测试运行战争、发射后不管、条件测试逻辑）在 75% 的案例中与[不稳定测试](https://www.edgepedia.org/zh/flaky-test)存在因果共现，而重构测试以消除这些坏味道，可修复与其存在因果共现的全部不稳定测试，即占总数 75% 的那些测试。<sup>[8](https://dl.acm.org/doi/abs/10.1007/s10664-019-09683-z)</sup> 一项对 55 个 Java 项目应用两种不稳定测试检测器的纵向研究识别出 245 个不稳定测试，其中 75%（245 个中的 184 个）在首次添加时就已经不稳定；当检测器对新添加或直接修改的测试运行时，检测覆盖率升至 85%，而其余 15% 源于其他更改，需要对所有测试运行检测器。<sup>[18](https://dl.acm.org/doi/10.1145/3428270)</sup>

**顺序与共享状态。** Jupiter 的默认是顺序、确定性执行；自 5.3 版本起，通过 junit.jupiter.execution.parallel.enabled 配置参数可选择启用并行测试执行，执行模式由 @Execution 注解控制。<sup>[13](https://junit.org/junit5/docs/5.3.1/user-guide/index.html)</sup>

**整体质量。** 一项分析 112 个 Java 仓库、对照已知良好实践模式衡量单元测试质量的研究得出结论：现实世界中 JUnit 单元测试的质量较低，测试经常不遵循众所周知的建议。<sup>[19](https://ieeexplore.ieee.org/document/8445039)</sup> 框架本身提供了补救措施：基于 7 个 JUnit 5 功能的重构可以消除 13 种测试坏味道，包括断言轮盘、测试代码重复和条件测试逻辑，将此类重构应用于真实测试代码的 38 个拉取请求在受访开发者中达到了 94% 的接受率。<sup>[16](https://ieeexplore.ieee.org/document/9769994)</sup> 一份源自现实开发者重构的目录识别出两个 JUnit 5 特有的测试重构和七个与版本无关的重构，包括针对“不恰当断言”坏味道的新重构。<sup>[20](https://link.springer.com/article/10.1007/s11219-024-09663-7)</sup>

## 近期发展

2025 年，JUnit 于 2 月发布 5.12，5 月发布 5.13；JUnit 6.0 原定于 9 月发布。<sup>[7](https://speakerdeck.com/marcphilipp/catching-up-with-junit)</sup> JUnit 5.12.0 于 2025 年 2 月 21 日发布，增加了测试和容器的输出文件附件、对 Open Test Reporting XML 输出的改进、测试超时时的线程转储，以及 JUnit Vintage 引擎中的并行执行支持。<sup>[6](https://docs.junit.org/5.12.0/release-notes/)</sup> Open Test Reporting 支持包括输出捕获、文件附件和新的 HTML 报告。<sup>[7](https://speakerdeck.com/marcphilipp/catching-up-with-junit)</sup>

JUnit 5.13 增加了对测试类（而不仅仅是方法）进行参数化的支持，这是 JUnit 问题跟踪器中得票最高的问题。<sup>[7](https://speakerdeck.com/marcphilipp/catching-up-with-junit)</sup> JUnit 6.0 将 Java 和 Kotlin 的基线分别提升至 17 和 2.x；当前 JUnit 6 一代要求 Java 17 和 Kotlin 2.1 或以上版本。<sup>[7](https://speakerdeck.com/marcphilipp/catching-up-with-junit)</sup><sup> • </sup><sup>[3](https://junit.org/)</sup> 发布系列持续进行，junit-team 仓库（已更名为 junit-framework）列出的版本直至 JUnit 6.1.3，该版本于 2026 年 8 月 7 日正式发布。<sup>[12](https://github.com/junit-team/junit-framework/releases)</sup>

## References

1. [Empirical Study of Test Case and Test Framework Presence in Public Projects on GitHub, Applied Sciences (MDPI)](https://www.mdpi.com/2076-3417/11/16/7250)
2. [JUnit 3.8 Documented Using Collaborations, Software Engineering Notes (2008)](https://dirkriehle.com/wp-content/uploads/2008/04/sen-2008-03-junit-doc.pdf)
3. [JUnit, official project site](https://junit.org/)
4. [JUnit 5 User Guide](https://docs.junit.org/5.11.0/user-guide/)
5. [Xunit, Martin Fowler](https://martinfowler.com/bliki/Xunit.html)
6. [JUnit 5 Release Notes 5.12.0](https://docs.junit.org/5.12.0/release-notes/)
7. [Catching up with JUnit, Marc Philipp (JUnit team)](https://speakerdeck.com/marcphilipp/catching-up-with-junit)
8. [The smell of fear: on the relation between test smells and flaky tests, Empirical Software Engineering](https://dl.acm.org/doi/abs/10.1007/s10664-019-09683-z)
9. [Interview with Kent Beck, Oracle Java Magazine](https://blogs.oracle.com/javamagazine/post/interview-with-kent-beck)
10. [An Introduction into JUnit, Karlsruhe Institute of Technology course notes](https://formal.kastel.kit.edu/teaching/pse/201011/junit.pdf)
11. [So Why is Spock Such a Big Deal?, ACCU Overload](https://www.accu.org/journals/overload/24/131/winder_2203/)
12. [Releases, junit-team/junit-framework on GitHub](https://github.com/junit-team/junit-framework/releases)
13. [JUnit 5 User Guide (5.3)](https://junit.org/junit5/docs/5.3.1/user-guide/index.html)
14. [JUnit: A Cook's Tour, Gamma and Beck](https://curlunit.sourceforge.net/doc/cookstour/cookstour.htm)
15. [Gradle Java Testing Guide](https://docs.gradle.org/current/userguide/java_testing.html)
16. [Test smells and JUnit 5 feature adoption: a mixed-method study, IEEE](https://ieeexplore.ieee.org/document/9769994)
17. [TestNG Documentation](https://testng.org/documentation.html)
18. [A large-scale longitudinal study of flaky tests, PACMPL](https://dl.acm.org/doi/10.1145/3428270)
19. [The Quality of JUnit Tests: An Empirical Study Report, IEEE](https://ieeexplore.ieee.org/document/8445039)
20. [A comprehensive catalog of refactoring strategies to handle test smells in Java-based systems, Software Quality Journal](https://link.springer.com/article/10.1007/s11219-024-09663-7)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 软件与编程 › Named software products and platforms › Software testing frameworks

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/junit

English original: https://www.edgepedia.org/junit
