Lab 09: Testing

Objective

Build a comprehensive test suite for ProductService using Java's assertion mechanisms: grouped tests, parametric data-driven testing, edge case coverage, approximate numeric comparisons, and expected-exception testing.

Background

JUnit 5 is the industry standard for Java testing, but its core patterns (grouping, parametrize, assertions, lifecycle) can be understood by building a minimal equivalent. This lab exercises the same test thinking you'll use with JUnit 5 — the patterns transfer directly. Understanding how tests work at the framework level makes you a better test author.

Time

25 minutes

Prerequisites

  • Lab 08 (Design Patterns)

Tools

  • Docker: zchencow/innozverse-java:latest


Lab Instructions

Steps 1–8: assertEquals, assertTrue, assertThrows, parametrize, edge cases, mocking, coverage, Capstone

💡 Parametrized tests eliminate copy-paste. Instead of writing one test per product, store expected values in a List<TestCase> (using a record!) and loop over them. JUnit 5's @ParameterizedTest with @MethodSource does exactly this — the pattern is identical, just with annotations. Fewer tests, more coverage, less maintenance.

📸 Verified Output:


Summary

Assertion
Checks

assertEquals(expected, actual)

Value equality

assertTrue(cond)

Condition is true

assertFalse(cond)

Condition is false

assertApprox(exp, act, tol)

Floating-point within tolerance

assertThrows(Type, fn)

Lambda throws expected exception

JUnit 5 equivalents: assertEquals, assertTrue, assertFalse, assertThat, assertThrows, @ParameterizedTest @MethodSource.

Further Reading

Last updated