The AAA-Principle for Unit-Tests

When you write unit-tests it's good practice to use the AAA-principle. It is derived from the initial letters of following actions:


Initialize all data needed to execute the test.
Here you create all variables and load data from external resources.


Execute the test
The execution of the test should only consist of a single line: the call of the function you want to test.


Compare the result with the expected value.
At last an assert-statement of the test-framework is called on the expected value and the result of the function under test. In JUnit it is called assertEquals or assertTrue, but also other options for verification are available.
You should only use one assert-statement in each testfunction, because a failing assertion stops execution and no other line of code will be executed. In that case it can be difficult to localize the origin of a failed test.