Testing
How to Make Your Tests Readable
This article explores how to write more readable and maintainable tests. It covers behavior-focused naming, structured test patterns, minimizing irrelevant details, using test data builders, and intent-revealing helpers to make tests easier to understand and maintain.
How to Create a Test Data Builder
This article explains how to build flexible and expressive test data using custom builders. It covers hiding irrelevant details, setting safe defaults, and combining builders with object mothers to simplify test code. Following these patterns makes tests easier to read, maintain, and adapt to changes in object structures.
DRY and DAMP in Tests
This article explores how to apply DRY and DAMP principles in tests without sacrificing readability. It demonstrates using test data builders to centralize object construction and custom assertions to simplify verification. These patterns help keep tests both maintainable and expressive.
How Do I Test Private Methods?
This article explains why private methods should be tested indirectly through public methods. It discusses how complex private logic can indicate design issues and how refactoring into smaller classes or using appropriate visibility can improve testability. Following this approach keeps tests maintainable and allows refactoring without breaking them.
Using Mockito With JUnit 5
This guide explains how to integrate Mockito with JUnit 5 for effective unit testing. It covers three approaches: manual mock creation, annotation-based initialization, and using the Mockito JUnit 5 extension for automatic mock injection. By following these methods, you can simplify test setup, improve readability, and validate mock usage efficiently.
JUnit 5 Test Lifecycle: Before and After Annotations
This tutorial explores how to run code before and after tests in JUnit 5, both for individual tests and across the entire test class. It explains the differences between per-test and per-class test instances, handling nested tests, and how extensions wrap test execution. Learning these patterns helps manage shared resources and maintain predictable test behavior.
JUnit 5 Expected Exception: How to assert an exception is thrown?
This tutorial explains how to confirm that a specific exception is thrown in JUnit 5 tests and how to inspect the exception message. It covers handling unexpected or missing exceptions and shows how capturing the exception allows for more detailed assertions. Following these practices ensures reliable and precise error-handling tests.
A More Practical Guide to JUnit 5 Parameterized Tests
This tutorial explains how to create parameterized tests in JUnit 5, allowing the same test to run with different inputs. It covers single and multiple arguments, using external data sources like CSV files, and custom conversions or aggregations for complex types. Parameterized tests help reduce duplication and make test code more maintainable and readable.
Getting Started with JUnit 5: Writing Your First Test
This guide walks you through writing a simple JUnit 5 test to ensure your code works as intended. You’ll learn how to set up tests, verify expected results, and run them using an IDE, Maven, or Gradle.
JUnit 5 Assertions: Verifying Test Results
This article covers how to use JUnit 5 assertions to validate test results, including values, booleans, nulls, arrays, objects, exceptions, and timeouts. It explains custom error messages and grouped assertions for clearer reporting. For advanced scenarios, it also introduces third-party libraries like Hamcrest, AssertJ, and Truth to make assertions more readable and expressive.