JUnit
Basics
Learn how to structure and write automated tests in Java using JUnit: the essential framework for verifying your code works correctly.
What Is Automated Testing?
Instead of manually running your program and checking output with your eyes, automated testing means writing small programs that verify your code for you. Each test runs a piece of your code, checks the result, and reports pass or fail instantly.
What Is JUnit?
Why Write Tests?
Anatomy of a JUnit Test
Core Annotations
Key Assertion Methods
| Method | What It Checks |
|---|---|
| assertEquals(a, b) | a equals b |
| assertNotEquals(a, b) | a does not equal b |
| assertTrue(cond) | condition is true |
| assertFalse(cond) | condition is false |
| assertNull(obj) | object is null |
| assertThrows(E, code) | code throws exception E |
@Test vs. main()
| JUnit @Test | main() Method | |
|---|---|---|
| Discovery | Automatic | Must call manually |
| Isolation | Each test runs independently | Shared state, order-dependent |
| Reporting | Pass/fail per method | Crashes or prints to console |
| Scalability | Runs hundreds in seconds | Tedious to maintain |
How to Write a Test
Example 1: Simple Test
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class CalculatorTest { @Test void shouldAddTwoNumbers() { // Arrange Calculator calc = new Calculator(); // Act int result = calc.add(3, 7); // Assert assertEquals(10, result); } }
Example 2: @BeforeEach Setup
import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; class CalculatorTest { private Calculator calc; @BeforeEach void setUp() { calc = new Calculator(); // fresh instance each test } @Test void testAdd() { assertEquals(5, calc.add(2, 3)); } @Test void testSubtract() { assertEquals(1, calc.subtract(3, 2)); } }
Example 3: Testing Exceptions
@Test void shouldThrowOnDivideByZero() { Calculator calc = new Calculator(); assertThrows( ArithmeticException.class, () -> calc.divide(10, 0) ); }
Test Lifecycle Flow
Test Naming Conventions
One Behavior Per Test
Each test method should verify exactly one behavior. If you find yourself writing multiple unrelated assertions in one method, split them into separate tests.
// BAD: testing two behaviors at once @Test void testCalculator() { assertEquals(5, calc.add(2, 3)); assertEquals(1, calc.subtract(3, 2)); // unrelated! } // GOOD: one behavior per test @Test void testAdd() { assertEquals(5, calc.add(2, 3)); } @Test void testSubtract() { assertEquals(1, calc.subtract(3, 2)); }
Common Mistakes
Question 1 of 5
Which annotation marks a method as a test case in JUnit 5?
Question 2 of 5
In assertEquals(expected, actual), what is the correct order of arguments?
Question 3 of 5
What does @BeforeEach do?
Question 4 of 5
Which assertion checks that code throws a specific exception?
Question 5 of 5
Consider this test method:
@Test
void testOperations() {
Calculator c = new Calculator();
assertEquals(5, c.add(2, 3));
assertEquals(6, c.multiply(2, 3));
assertEquals(1, c.subtract(3, 2));
}
What is the main problem with this test?