Photo by Chunlea Ju on Unsplash

Unit Testing — AAA pattern

Supreet Singh
Nov 6 · 3 min read

Unit testing is one of the Extreme programming practice in which individual units of source code are tested in an automated manner.

Unit tests are an essential tool for any software developer. They help us create a safety net that we can rely on for safe refactoring and ensuring we don’t introduce any regressions. At Xebia, we are required to write unit tests for any functional code we write. As a junior developer, it took me time to understand that writing unit tests is different from writing maintainable unit tests. It takes time and effort to master the art of writing maintainable tests. In this blog, I will share AAA pattern that has helped me develop a mental model on how to layout my tests.

Understanding AAA pattern

AAA pattern suggests that we should layout our tests into three sections — Arrange, Act, Assert. Each of the part is responsible for a specific concern of a test case. Let’s look at them one by one.

Arrange: In this section, we perform the setup and initialization required for the test. This usually involves creating data required for a test case and/or setting up mocks,

Act: In this section, we take the actions required for a test case. This means calling the method under test.

Assert: In this section, we verify that our test case did what was expected from it. This usually involves asserting the response of the method under test and/or verify that invocations were made on mock objects.

The AAA pattern helps you keep your unit tests well structured, clean and readable. This pattern stays the same, regardless of what you are mocking.

This pattern has several significant benefits. Some of the benefits are:

  1. It creates a clear separation between a test’s setup, operations, and results. It also enforces a certain degree of discipline when you write your tests.
  2. The well-defined structure makes the code easier to read and understand. If you place the steps in order and format your code to separate them, you can scan a test and quickly comprehend what it does.

Mastering AAA with an Example

Let us take an example to understand how we can apply arrange, act and assert pattern in the real-world. Suppose we have a method that returns the reverse of a string.

public static String reverse(String str) {
StringBuilder input = new StringBuilder();
input= input.reverse();
return input.toString();

Now we will write the test cases for this method using the AAA pattern. We should carefully write all the test and make sure that all the boundary cases are covered.

So the very first thing we should take care of while writing the test is, the name of the test should be very clear and should always make sense. As we can see we divided our test into three parts. First is arranging where we have arranged all the credentials for the test like what should be our input and what should be our expected output.

Secondly, we called the reverse method with input. Since our method is static here so we don’t need instantiation part as we can simply call the reverse method.

And the last part is the assertion where we compare the actual and expected results.

Xebia Engineering Blog

The Xebia Engineering Blog

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade