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

JUnit-Assertions und Testausführung

Lernen Sie, die Assertion-Methoden von JUnit zur Validierung von Testergebnissen zu verwenden und Tests effektiv in Ihrer IDE auszuführen.

JUnit-Assertions und Testausführung ist eine kostenlose Testing Mastery: JUnit, Mockito & Integration Tests-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Testing Mastery: JUnit, Mockito & Integration Tests-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Testing Mastery: JUnit, Mockito & Integration Tests-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

What are Assertions?

Assertions are the checks that decide pass or fail: they verify a condition is true, and if it isn’t, the test fails and flags the problem.

Checking for Equality

assertEquals() is the workhorse: pass it the expected value and the actual value, and the test passes only when they match.

`assertEquals` in Action

Here’s assertEquals verifying a sum: compare the expected result against what your method returns. Match means pass.

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class MathUtilsTest {

    // A simple method to test
    int add(int a, int b) {
        return a + b;
    }

    @Test
    void testAddMethod() {
        // Expected result: 5
        int expected = 5;
        // Actual result from our method
        int actual = add(2, 3);
        
        // Assert that expected and actual are the same
        assertEquals(expected, actual, "2 + 3 should be 5");
    }
}

Checking Boolean Conditions

To check logical outcomes, use assertTrue and assertFalse — they pass when the condition is true or false respectively.

`assertTrue` in Practice

Here assertTrue confirms a number is positive — a clean way to validate any boolean condition.

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class NumberCheckerTest {

    // A simple method to test
    boolean isPositive(int number) {
        return number > 0;
    }

    @Test
    void testIsPositive() {
        int num = 10;
        // Assert that num is positive
        assertTrue(isPositive(num), num + " should be positive");
    }
}

Handling Null Values

For null checks, use assertNotNull and assertNull — crucial for catching NullPointerExceptions before they bite.

`assertNotNull` Demo

Here assertNotNull and assertNull verify a lookup that returns a user or null when none is found.

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

public class UserServiceTest {

    // Dummy method simulating fetching a user
    String getUserById(int id) {
        if (id == 1) {
            return "Alice"; // User found
        }
        return null; // User not found
    }

    @Test
    void testExistingUser() {
        String user = getUserById(1);
        assertNotNull(user, "User with ID 1 should exist");
    }

    @Test
    void testNonExistingUser() {
        String user = getUserById(99);
        assertNull(user, "User with ID 99 should not exist");
    }
}

Executing JUnit Tests

Running tests is easy: most IDEs have built-in JUnit support — right-click a class or method, hit Run, and read the results window.

Understanding Pass/Fail

Reading results is simple: green means all tests passed, red means an assertion failed and there’s likely a bug. Click a failure for details.

Assertion Challenge

Consider the following simple method and test. What will be the outcome of running this test?

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class SimpleCalculatorTest {
    int subtract(int a, int b) {
        return a - b;
    }

    @Test
    void testSubtract() {
        assertEquals(10, subtract(15, 5));
    }
}

Recap: Assertions & Execution

You can now validate code with JUnit assertions — assertEquals, assertTrue, assertNull and friends — and run tests right in your IDE.

Häufig gestellte Fragen

Ist die Lektion „JUnit-Assertions und Testausführung“ kostenlos?

Ja — der vollständige Text von „JUnit-Assertions und Testausführung“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Testing Mastery: JUnit, Mockito & Integration Tests-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Testing Mastery: JUnit, Mockito & Integration Tests-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „JUnit-Assertions und Testausführung“?

Lernen Sie, die Assertion-Methoden von JUnit zur Validierung von Testergebnissen zu verwenden und Tests effektiv in Ihrer IDE auszuführen. Du übst Testing Mastery: JUnit, Mockito & Integration Tests mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Testing Mastery: JUnit, Mockito & Integration Tests zu starten?

Keine Vorkenntnisse erforderlich. Testing Mastery: JUnit, Mockito & Integration Tests auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.

Wie lange dauert die Lektion „JUnit-Assertions und Testausführung“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Testing Mastery: JUnit, Mockito & Integration Tests-Lektion Code schreiben und ausführen?

Ja. Jede Testing Mastery: JUnit, Mockito & Integration Tests-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Einführung in Unit-Tests
  2. Grundlegende JUnit-5-Annotations
  3. JUnit-Assertions und Testausführung
  4. Tests mit @DisplayName und Verschachtelung organisieren
← Zurück zu Testing Mastery: JUnit, Mockito & Integration Tests