0Pricing
Testing Mastery: JUnit, Mockito & Integration Tests · Lekcja

Podstawowe adnotacje JUnit 5

Poznaj najważniejsze adnotacje JUnit 5, takie jak @Test, @BeforeEach i @AfterEach, oraz ich praktyczne zastosowania.

Podstawowe adnotacje JUnit 5 to bezpłatna lekcja Testing Mastery: JUnit, Mockito & Integration Tests na CoddyKit. To lekcja 2 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Testing Mastery: JUnit, Mockito & Integration Tests, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Testing Mastery: JUnit, Mockito & Integration Tests zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.

Często zadawane pytania

Czy lekcja „Podstawowe adnotacje JUnit 5” jest bezpłatna?

Tak — pełny tekst „Podstawowe adnotacje JUnit 5” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Testing Mastery: JUnit, Mockito & Integration Tests, przejdź na CoddyKit PRO. Kurs Testing Mastery: JUnit, Mockito & Integration Tests zawiera 4 lekcji w sumie.

Co nauczysz się w „Podstawowe adnotacje JUnit 5”?

Poznaj najważniejsze adnotacje JUnit 5, takie jak @Test, @BeforeEach i @AfterEach, oraz ich praktyczne zastosowania. Ćwiczysz Testing Mastery: JUnit, Mockito & Integration Tests z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Testing Mastery: JUnit, Mockito & Integration Tests?

Nie wymagamy żadnego doświadczenia. Testing Mastery: JUnit, Mockito & Integration Tests w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 2 z 4.

Ile czasu zajmuje lekcja „Podstawowe adnotacje JUnit 5”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Testing Mastery: JUnit, Mockito & Integration Tests?

Tak. Każda lekcja Testing Mastery: JUnit, Mockito & Integration Tests zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Wprowadzenie do testów jednostkowych
  2. Podstawowe adnotacje JUnit 5
  3. Asercje i wykonywanie testów w JUnit
  4. Organizowanie testów za pomocą @DisplayName i zagnieżdżania
← Powrót do Testing Mastery: JUnit, Mockito & Integration Tests