使用 @DisplayName 与嵌套组织测试
使用描述性的显示名称和用于归类相关场景的嵌套测试类,让您的 JUnit 5 测试套件清晰且结构良好。
使用 @DisplayName 与嵌套组织测试 是 CoddyKit 上的免费 Testing Mastery: JUnit, Mockito & Integration Tests 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Testing Mastery: JUnit, Mockito & Integration Tests 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Testing Mastery: JUnit, Mockito & Integration Tests 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Tests Are Documentation
A test suite is more than a safety net — it documents how your code should behave. JUnit 5’s @DisplayName and @Nested make that documentation shine.
The Problem with Method Names
A name like testAdd2 says almost nothing, and method names can’t hold spaces or punctuation — so they can only be so expressive.
@Test
void testAdd2() { /* ... */ }Introducing @DisplayName
@DisplayName gives a test any readable label — spaces, punctuation, even emoji. Reports show the label instead of the method name.
@Test
@DisplayName("adds two positive numbers correctly")
void addsTwoPositives() {
assertEquals(5, calc.add(2, 3));
}Naming the Class Too
Put @DisplayName on the test class too, to describe the whole unit under test in plain language.
@DisplayName("Calculator")
class CalculatorTest { /* ... */ }Display Name Generators
Don’t want to annotate every method? A display name generator turns method names into readable text automatically.
@DisplayNameGeneration(
DisplayNameGenerator.ReplaceUnderscores.class)
class CalculatorTest { /* ... */ }Why Nested Tests?
Related tests often share context — “cart is empty” vs “cart has items.” @Nested classes group them so structure mirrors behavior.
Creating a Nested Class
Annotate an inner, non-static class with @Nested and its tests run inside the outer class’s lifecycle.
@Nested
@DisplayName("when cart is empty")
class WhenEmpty {
@Test
void totalIsZero() { assertEquals(0, cart.total()); }
}Shared Setup per Group
Each nested class can have its own @BeforeEach, setting up that group’s specific context without touching its siblings.
@Nested
class WhenHasItems {
@BeforeEach
void addItems() { cart.add(item); }
}Readable Test Reports
Together, display names and nesting produce reports that read like specs: “Calculator → when adding → adds two positives.” Intent at a glance.
Behavior-Driven Structure
This structure naturally fits a given-when-then style, so your tests double as a living behavior spec for the class under test.
Best Practices
Write self-explaining suites: name tests by behavior not method, group scenarios with @Nested, give each its own setup, aim for sentence-like reports.
Quick Check
@DisplayName or @Nested — which one builds the spec-like report?
Recap
You organized tests for clarity: @DisplayName labels them, generators automate naming, and @Nested groups scenarios into a readable spec.
常见问题解答
「使用 @DisplayName 与嵌套组织测试」课时是免费的吗?
是的 — 「使用 @DisplayName 与嵌套组织测试」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Testing Mastery: JUnit, Mockito & Integration Tests 课程的其余内容,请升级到 CoddyKit PRO。 Testing Mastery: JUnit, Mockito & Integration Tests 课程共包含 4 节课。
「使用 @DisplayName 与嵌套组织测试」这节课中我会学到什么?
使用描述性的显示名称和用于归类相关场景的嵌套测试类,让您的 JUnit 5 测试套件清晰且结构良好。 你通过在浏览器中直接运行的动手代码来练习 Testing Mastery: JUnit, Mockito & Integration Tests,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Testing Mastery: JUnit, Mockito & Integration Tests 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Testing Mastery: JUnit, Mockito & Integration Tests 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用 @DisplayName 与嵌套组织测试」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Testing Mastery: JUnit, Mockito & Integration Tests 课中编写并运行代码吗?
能。每节 Testing Mastery: JUnit, Mockito & Integration Tests 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 单元测试入门
- JUnit 5 基本注解
- JUnit 断言与执行
- 使用 @DisplayName 与嵌套组织测试