How to Structure Unit Test Method?

In this tutorial, you will learn about a way or a pattern to structure your code inside of a unit test method. If you follow this pattern consistently, then it will be easier to read and understand code inside of your test methods.

This pattern is widely used by many developers and is know as ArrangeActAssert(AAA).

Arrange, Act, Assert – AAA

The AAA(Arrange, Act, Assert) pattern is a common pattern for structuring code inside of a Unit Test.

Bel0w is an example of JUnit test method that uses the AAA pattern.

@Test    
@DisplayName("Test 4/2 = 2")
void testIntegerDivision_WhenFourIsDividedByTwo_ShouldReturnTwo() {
    // Arrange 
    int dividend = 4;
    int divisor = 2;
    int expectedResult = 2;

    // Act 
    int actualResult = calculator.integerDivision(dividend, divisor);

    // Assert 
    assertEquals(expectedResult, actualResult, "4/2 did not produce 2");
}

Arrange

The very first section of your test method, that is called Arrange. This is is where you prepare and initialize all the needed variables and objects that are needed by your System Under Test to work and to return the expected result.

For example, in the code example above, we are testing the integerDivision() method. To invoke the integerDivision() method, we need an object of Calculator class.  So we use the Arrange section to prepare this object.

 Act

The next main section of your Unit Test method is called Act. In the Act section you will actually invoke the method that you are testing. In the code example above, the method under test is called integerDivision() and it is the the integerDivision() method that we are invoking in the Act section.

Arrange

The next main section of your Unit Test method is called Assert and it will be used to validate the return value received from the integerDivision() method. If the returned value is what is expected and is correct, then the test method will pass. If the returned value is not what was expected, then the test method will fail.

Alright, so these are the three main sections of your Unit test method. And they are meant to help us to better organize code in our unit test. If you follow this pattern consistently in all of your unit tests, then it will be easier to read them.

Conclusion

In conclusion, mastering the art of structuring unit test methods is essential for any Java developer seeking to ensure the reliability and quality of their code. By following the Arrange, Act, Assert (AAA) pattern, developers can organize their tests effectively and enhance readability.

The “Arrange” phase allows for setting up the necessary preconditions, while the “Act” phase performs the actual method invocation or interaction. Finally, the “Assert” phase validates the expected outcomes and asserts the correctness of the code under test. Looking to enhance your unit testing skills? Visit the Testing Java Code page to become proficient in creating well-organized and efficient test cases.

Video Tutorial