1. Concept library
  2. Java & Spring Boot
  3. Module 5: Build tools and testing

Concept

JUnit tests

JUnit runs methods annotated with @Test and reports which assertions failed. Parameterised tests cover a table of cases, and assertThrows checks error paths.

What a session covers

The tutor works through these with you, in conversation and code. A session ends when you have shown them, not when a video does.

  1. Write a test with @Test and a clear assertion
  2. Name tests after the behaviour they check
  3. Cover a table of cases with a parameterised test

In practice

A few lines that show JUnit tests doing its job.

Java
class MoneyTest {
    @Test
    void addsSameCurrency() {
        var sum = new Money("USD", 100).plus(new Money("USD", 250));
        assertEquals(350, sum.cents());
    }

    @ParameterizedTest
    @ValueSource(longs = {-1, -100})
    void rejectsNegativeAmounts(long cents) {
        assertThrows(IllegalArgumentException.class, () -> new Money("USD", cents));
    }
}

The common mistake

Watch for

Naming tests test1, test2, or after the method under test. When one fails, the name should say which behaviour broke: rejectsNegativeAmounts tells you; testPlus does not.

What to know first

Your roadmap puts these before JUnit tests, unless you already know them.

How it stays learned

Once a session shows you understand JUnit tests, it comes back for review. Each review takes a minute and tells the memory model how well you still know it.

  1. Day 3First recall, while it is fresh
  2. Day 7Second, a week on
  3. Day 18Last, when it has settled

Where it leads

Topics that build on JUnit tests.

Previous in the module← Maven and Gradle
Next in the moduleMocking with Mockito →