AI Hints
Home›Spring Boot Tutorial›Why Test, JUnit Basics and Testing a Service
Spring Boot · Lesson 100 of 120

Why Test

Confidence to change code.

⏱ 2 min read▶ Video at 2:12:27🎓 Free
Watch this lesson in the Spring Boot courseWatch on YouTube ↗Full Spring Boot course

Every time you change code, something else might break. Clicking through the whole app after every change is slow, and you'll miss things. Automated tests check it for you, in seconds.

  • Changes can break things
  • Manual checks are slow
  • Tests check in seconds

What tests give you

Tests catch mistakes early, before users do. They let you change code with confidence. They document how the code should behave. And they run automatically, every time you build.

keyvalue
Catch bugsbefore users do
Confidencechange code safely
Documentationhow it should behave
Automaticon every build

Kinds of test

Unit tests check one class, alone, and they're very fast. Slice tests start part of Spring, like just the web layer. And integration tests start the whole app. Write many unit tests, some slice tests, and a few integration tests.

KindChecksSpeed
Unitone class alonemilliseconds
Sliceone layer of Springabout a second
Integrationthe whole appseconds

The class we will test

Price calculator takes a percentage off a price. It rejects a percentage below zero, or above one hundred. And it rounds to two decimal places.

PriceCalculator.javajava
@Service
public class PriceCalculator {

    public double discounted(double price, int percent) {
        if (percent < 0 || percent > 100) {
            throw new IllegalArgumentException("Bad percent: " + percent);
        }
        return Math.round(price * (100 - percent)) / 100.0;
    }
}

A first test

Test classes live in source test java. Each method marked at Test is one test. Assert equals checks, that the actual value equals the expected one. Run it. Maven runs both tests. Both pass, and the build succeeds.

test/PriceCalculatorTest.javajava
class PriceCalculatorTest {

    PriceCalculator calc = new PriceCalculator();

    @Test
    void takesTwentyPercentOff() {
        assertEquals(8.0, calc.discounted(10.0, 20));
    }

    @Test
    void noDiscountKeepsThePrice() {
        assertEquals(9.99, calc.discounted(9.99, 0));
    }
}
Output
[INFO] Running PriceCalculatorTest
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS
Also uses PriceCalculator.java
@Service
public class PriceCalculator {

    public double discounted(double price, int percent) {
        if (percent < 0 || percent > 100) {
            throw new IllegalArgumentException("Bad percent: " + percent);
        }
        return Math.round(price * (100 - percent)) / 100.0;
    }
}
Also uses _mvn
test

Summary

Automated tests catch mistakes early, and let you change code with confidence. Write many fast unit tests. Next, JUnit basics.

Key takeaways
  • Unit, slice, integration
  • @Test
  • ./mvnw test
Next up: JUnit Basics — tests that check themselves