Skip to content
Veltoryx Technologies
ProLesson 12.1Intermediate3 min readUpdated 30 October 2026

Unit testing with JUnit 5

Write your first unit tests with JUnit 5, use assertions and parameterised tests, test exceptions, and understand where mocks fit.

A unit test is a small piece of code that checks one piece of your code, usually one method, gives the right result. Tests catch bugs before users do, let you change code without fear, and are expected in every professional Java job. JUnit 5 is the standard tool.

01The code we'll test

PriceCalculator.java
public class PriceCalculator {
    public double finalPrice(double price, int discountPercent) {
        if (price < 0) throw new IllegalArgumentException("price must not be negative");
        if (discountPercent < 0 || discountPercent > 100) {
            throw new IllegalArgumentException("discount must be 0-100");
        }
        return price * (100 - discountPercent) / 100;
    }
}

Loading the rest of the lesson…