Basic JUnit 5 Annotations
Explore essential JUnit 5 annotations like @Test, @BeforeEach, @AfterEach, and their practical applications.
Basic JUnit 5 Annotations is a free Testing Mastery: JUnit, Mockito & Integration Tests lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Testing Mastery: JUnit, Mockito & Integration Tests learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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.
Frequently asked questions
Is the “Basic JUnit 5 Annotations” lesson free?
Yes — the full text of “Basic JUnit 5 Annotations” is free to read here on the web, and the Testing Mastery: JUnit, Mockito & Integration Tests course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Testing Mastery: JUnit, Mockito & Integration Tests course, upgrade to CoddyKit PRO.
What will I learn in “Basic JUnit 5 Annotations”?
Explore essential JUnit 5 annotations like @Test, @BeforeEach, @AfterEach, and their practical applications. You practise Testing Mastery: JUnit, Mockito & Integration Tests with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Testing Mastery: JUnit, Mockito & Integration Tests?
No prior experience is required. Testing Mastery: JUnit, Mockito & Integration Tests on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Basic JUnit 5 Annotations” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Testing Mastery: JUnit, Mockito & Integration Tests lesson?
Yes. Every Testing Mastery: JUnit, Mockito & Integration Tests lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.