Free preview.You're sampling one lesson — enroll free to unlock all 8 lessons and track your progress.
Enroll free
lesson

Why and What to Test

Why and What to Test

In this lesson — part of Testing Fundamentals — you'll learn why and what to test in Java and why it matters in real work.

Why it matters

Tests prove your code works and keep it working as you change it.

Key ideas

  • What to test
  • Arrange-Act-Assert
  • Running a test suite
  • Good vs. brittle tests

In practice

Here's how it looks in idiomatic Java:

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

class MathTest {
    @Test
    void addsCorrectly() {
        assertEquals(5, 2 + 3);     // fails the test if not equal
    }
}

Java note: JUnit 5 (Jupiter) is the de facto standard: test methods are marked with @Test and assertions like assertEquals(expected, actual) put the expected value first.

Try it yourself

Exercise: In Java, write three tests for a function that reverses a string.

Recap

You now understand why and what to test and can apply it in Java. Mark this lesson complete and continue to the next one.