Arrange Act Assert Pattern

It’s a pattern for arranging and formatting unit test methods. The main benefit of this pattern is that it clearly separates what is being tested from setup and verification steps.

The below are the steps defined in this pattern.

1. Arrange all necessary preconditions and inputs.
2. Act on the object or method under test.
3. Assert that the expected results have occurred.

Example unit test method is given below.


@Test
 public void testAdd() {
    int a = 10;
    int b = 10;
    		
    int c = Calculate.add(a, b);
    
    assertEquals(20, c);
 }

The above method is used for testing the “add” method defined in Calculate.java. In the first 2 lines, the input parameters(Arrange) are defined and in the 3rd line, the add method is getting called with the given input parameters(Act) and in the 4th line, the result value is getting verified(Assert).