This structure makes the code easier to read and understand. You should format your code to separate these steps.
For example we have a class that sums up two numbers:
public class SumUp {
public int add (int firstNumber, int secondNumber) {
return firstNumber+secondNumber;
}
}
And we have a class with tests that are created according to AAA. Creation of the object and initialization of int - is an Arrange part. Calling the method sumUp.add() is an Act part. And assert is an Assert part.
public class SumUpTests {
@Test
public void testSum() {
SumUp sumUp = new SumUp();
int firstNumber = 5;
int secondNumber = 2;
int result = sumUp.add(firstNumber, secondNumber);
Assert.assertEquals(result, 7);
}
}
Summary:
1 – Easier to read, maintain, and enhance
2 – Useful for unit tests