JUnit 5 基本注解
探索基本的 JUnit 5 注解,例如 @Test、@BeforeEach、@AfterEach,以及它们的实际应用。
JUnit 5 基本注解 是 CoddyKit 上的免费 Testing Mastery: JUnit, Mockito & Integration Tests 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Testing Mastery: JUnit, Mockito & Integration Tests 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Testing Mastery: JUnit, Mockito & Integration Tests 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What are JUnit Annotations?
In JUnit 5, annotations are markers that tell the runner how to run your tests — defining test methods, setup, and cleanup.
Marking Tests with @Test
The core annotation is @Test. Put it above any method and JUnit runs it as a test, reporting pass or fail.
Your First Test Method
Here’s @Test in action: a method that asserts 1 + 1 equals 2. (Assertions like assertEquals are coming up next lesson.)
package com.coddykit;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class SimpleTest {
@Test
void additionTest() {
int result = 1 + 1;
assertEquals(2, result, "1 + 1 should be 2");
}
}Setup Before Each Test: @BeforeEach
@BeforeEach runs before every test method, giving each one a fresh, known state — so your tests stay independent and reliable.
@BeforeEach in Practice
Here @BeforeEach resets a message before each test runs, so both tests start from the same clean state.
package com.coddykit;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
class BeforeEachTest {
String message;
@BeforeEach
void setup() {
message = "Hello CoddyKit!";
System.out.println("Setup called."); // For illustration
}
@Test
void testMessageStartsWithHello() {
assertTrue(message.startsWith("Hello"));
}
@Test
void testMessageLength() {
assertTrue(message.length() > 5);
}
}Cleanup After Each Test: @AfterEach
@AfterEach runs after every test method — perfect for closing files, releasing connections, or resetting state so tests don’t bleed into each other.
@AfterEach in Action
Here @AfterEach tears down state after each test, clearing the log so the next test starts clean.
package com.coddykit;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
class AfterEachTest {
StringBuilder log;
@BeforeEach
void setup() {
log = new StringBuilder("Test started. ");
}
@Test
void testLogAppendOne() {
log.append("Action 1. ");
assertTrue(log.toString().contains("Action 1"));
}
@Test
void testLogAppendTwo() {
log.append("Action 2. ");
assertTrue(log.toString().contains("Action 2"));
}
@AfterEach
void teardown() {
System.out.println("Log after test: " + log.toString());
log = null; // Simulate cleanup
}
}Understanding the Test Flow
The lifecycle for each test is fixed: @BeforeEach, then the @Test method, then @AfterEach — repeating per test to guarantee isolation.
Order of Execution
Arrange the following actions in the correct order as JUnit executes them for each @Test method.
Recap: Essential JUnit Annotations
You’ve learned the essentials: @Test marks a test, @BeforeEach sets up, and @AfterEach cleans up. Assertions are up next.
常见问题解答
「JUnit 5 基本注解」课时是免费的吗?
是的 — 「JUnit 5 基本注解」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Testing Mastery: JUnit, Mockito & Integration Tests 课程的其余内容,请升级到 CoddyKit PRO。 Testing Mastery: JUnit, Mockito & Integration Tests 课程共包含 4 节课。
「JUnit 5 基本注解」这节课中我会学到什么?
探索基本的 JUnit 5 注解,例如 @Test、@BeforeEach、@AfterEach,以及它们的实际应用。 你通过在浏览器中直接运行的动手代码来练习 Testing Mastery: JUnit, Mockito & Integration Tests,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Testing Mastery: JUnit, Mockito & Integration Tests 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Testing Mastery: JUnit, Mockito & Integration Tests 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「JUnit 5 基本注解」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Testing Mastery: JUnit, Mockito & Integration Tests 课中编写并运行代码吗?
能。每节 Testing Mastery: JUnit, Mockito & Integration Tests 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 单元测试入门
- JUnit 5 基本注解
- JUnit 断言与执行
- 使用 @DisplayName 与嵌套组织测试