Anotasi Dasar JUnit 5
Jelajahi anotasi penting JUnit 5 seperti @Test, @BeforeEach, @AfterEach serta penerapan praktisnya.
Anotasi Dasar JUnit 5 adalah pelajaran Testing Mastery: JUnit, Mockito & Integration Tests gratis di CoddyKit. Ini adalah pelajaran 2 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Testing Mastery: JUnit, Mockito & Integration Tests, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Testing Mastery: JUnit, Mockito & Integration Tests mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
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.
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Anotasi Dasar JUnit 5” gratis?
Ya — teks lengkap “Anotasi Dasar JUnit 5” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Testing Mastery: JUnit, Mockito & Integration Tests, upgrade ke CoddyKit PRO. Kursus Testing Mastery: JUnit, Mockito & Integration Tests mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Anotasi Dasar JUnit 5”?
Jelajahi anotasi penting JUnit 5 seperti @Test, @BeforeEach, @AfterEach serta penerapan praktisnya. Kamu berlatih Testing Mastery: JUnit, Mockito & Integration Tests dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Testing Mastery: JUnit, Mockito & Integration Tests?
Tidak diperlukan pengalaman sebelumnya. Testing Mastery: JUnit, Mockito & Integration Tests di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 2 dari 4.
Berapa lama pelajaran “Anotasi Dasar JUnit 5” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Testing Mastery: JUnit, Mockito & Integration Tests ini?
Ya. Setiap pelajaran Testing Mastery: JUnit, Mockito & Integration Tests menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Pengantar Pengujian Unit
- Anotasi Dasar JUnit 5
- Asersi dan Eksekusi JUnit
- Mengatur Pengujian dengan @DisplayName dan Nesting