Search⌘ K
AI Features

Introduction: Writing Our First Test

Explore how to write your first unit test using test-driven development in Java. Understand key principles to organize readable tests, drive design decisions before implementation, and use tools like JUnit 5 and AssertJ to develop reliable code.

We'll cover the following...

Learn to write tests

It’s time for us to dive in and write our first TDD unit test in this chapter. To do this, we’ll learn about a simple template that helps us organize each test into a logical, readable piece of code. Along the way, we’ll learn some key principles we can use to make our tests effective. We’ll see how writing the test first forces us to make decisions about the design of our code and its ease of use, before needing to think about implementation details.

Chapter goals

After some examples covering those techniques, we’ll start working on our Wordz application, writing a test first before adding production code to make that test pass. We’ll use the popular Java unit testing libraries JUnit 5 and AssertJ to help us write easy-to-read tests.

In this chapter, we’ll cover the following main principles behind writing effective unit tests: