0Pricing
Testing Mastery: JUnit, Mockito & Integration Tests · บทเรียน

คำกำกับพื้นฐานของ JUnit 5

สำรวจคำกำกับสำคัญของ JUnit 5 เช่น @Test, @BeforeEach, @AfterEach และการประยุกต์ใช้ในทางปฏิบัติ

คำกำกับพื้นฐานของ JUnit 5 เป็นบทเรียน Testing Mastery: JUnit, Mockito & Integration Tests ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส 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 ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Testing Mastery: JUnit, Mockito & Integration Tests หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Testing Mastery: JUnit, Mockito & Integration Tests บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “คำกำกับพื้นฐานของ JUnit 5” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Testing Mastery: JUnit, Mockito & Integration Tests นี้ได้ไหม

ได้ บทเรียน Testing Mastery: JUnit, Mockito & Integration Tests ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. แนะนำการทดสอบหน่วย
  2. คำกำกับพื้นฐานของ JUnit 5
  3. การยืนยันผลและการดำเนินการของ JUnit
  4. การจัดระเบียบการทดสอบด้วย @DisplayName และการซ้อนกลุ่ม
← กลับไปที่ Testing Mastery: JUnit, Mockito & Integration Tests