Intermediate level · Module 5 of 6 · 13 guided hours
Unit Testing with JUnit 5
Write automated tests that check your code's behaviour, including edge cases and exceptions, and use them to fix bugs safely.
Learning objectives
- Write and run unit tests with JUnit 5
- Test edge cases and expected exceptions
- Use tests to reproduce and fix bugs and to refactor safely
Topics
- @Test and assertions
- Arrange–Act–Assert
- @BeforeEach
- assertThrows
- parameterized tests
- regression tests
Lessons
-
Lesson 1 Your First JUnit Tests
Write test methods with @Test and assertions, run them in IntelliJ and with Maven, and read the results.
@TestassertEqualsArrange–Act–Asserttest runner
-
Lesson 2 Testing Edge Cases and Exceptions
Test boundaries and error paths, check that exceptions are thrown, and remove duplication with @BeforeEach and parameterized tests.
boundary valueassertThrows@BeforeEach@ParameterizedTest
-
Lesson 3 Tests as a Safety Net: Bugs and Refactoring
Reproduce a reported bug with a failing test, fix it, and use the test suite to change code structure without changing behaviour.
regression testred–green–refactorrefactoringtest coverage
Practical activity
Write a test suite for the wallet or loan rules, find two seeded bugs through failing tests and fix them.
Expected outcomes
- Automated tests for business rules
- A habit of writing a failing test before fixing a bug
Assessment
Practical: write tests for a provided class; marked on the cases covered (normal, boundary, error) and on whether the tests detect seeded bugs.
Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.