Starting TDD: Arrange-Act-Assert
Explore the fundamentals of writing unit tests in Java using the Arrange-Act-Assert structure. Understand how TDD encourages designing callable interfaces first and improves workflow efficiency by validating code correctness early.
Unit tests
Unit tests are nothing mysterious. They’re just code, executable code written in the same language that we write our application in. Each unit test forms the first use of the code we want to write. It calls the code just as it will be called in the real application. The test executes that code, captures all the outputs that we care about, and checks that they are what we expected them to be. Because the test uses our code in the exact same way that the real application will, we get instant feedback on how easy or difficult our code is to use.
This might sound obvious, and it is, but it is a powerful tool that helps us write clean and correct code. Let’s take a look at an example of a unit test and learn how to define its structure.
Defining the test structure
It’s always helpful to have templates to follow when we do things and unit tests are no exception. Based on commercial work done on the Chrysler ...