The Arrange-Act-Assert pattern helps organize tests clearly. It makes tests easy to read and understand by separating setup, action, and verification.
When writing a new unit test for a method or function.
When you want to clearly show what is being tested and how.
When debugging tests to find which step fails.
When teaching others how to write clean and simple tests.
When maintaining tests to quickly understand their purpose.
Syntax
JUnit
/* Arrange */
// Prepare objects andset initial state
/* Act */
// Call the method or perform the action to test
/* Assert */
// Check the results with assertions
Arrange: Set up everything needed for the test.
Act: Perform the action you want to test.
Assert: Verify the outcome of the action.
Examples
This test checks if adding 2 and 3 returns 5.
JUnit
@Test
void testSum() {
// Arrange
Calculator calc = new Calculator();
// Act
int result = calc.add(2, 3);
// Assert
assertEquals(5, result);
}
2. Which of the following is the correct order of steps in the Arrange-Act-Assert pattern in a JUnit test?
easy
A. Arrange, Act, Assert
B. Assert, Arrange, Act
C. Act, Arrange, Assert
D. Act, Assert, Arrange
Solution
Step 1: Recall the pattern sequence
The pattern always starts with Arrange (setup), then Act (execute), and finally Assert (verify).
Step 2: Match the correct order
Only Arrange, Act, Assert lists the steps in the correct order: Arrange, Act, Assert.
Final Answer:
Arrange, Act, Assert -> Option A
Quick Check:
Order = Arrange, Act, Assert [OK]
Hint: Think: Prepare, Perform, Prove in order [OK]
Common Mistakes:
Mixing up the order of Act and Assert
Starting with Assert before setup
Skipping the Arrange step
Confusing Act with Assert
3. Consider the following JUnit test code snippet:
void testSum() {
int a = 5;
int b = 3;
int result = Calculator.add(a, b);
assertEquals(8, result);
}
Which part of the Arrange-Act-Assert pattern does int result = Calculator.add(a, b); represent?
medium
A. Arrange
B. Cleanup
C. Assert
D. Act
Solution
Step 1: Identify the code purpose
The line calls the method under test to perform the action, storing the output.
Step 2: Match to pattern step
This is the 'Act' step where the test executes the behavior to be tested.
Final Answer:
Act -> Option D
Quick Check:
Calling method = Act step [OK]
Hint: Act is the step where you call the method under test [OK]
Common Mistakes:
Confusing Act with Arrange (setup)
Thinking assertEquals line is Act
Assuming variable declarations are Act
Selecting Cleanup which is not part of AAA
4. Identify the error in this JUnit test following the Arrange-Act-Assert pattern:
@Test
void testMultiply() {
int x = 4;
int y = 2;
int product = x * y;
assertEquals(8, x * y);
}
medium
A. The Act step is missing
B. The Arrange step is incorrect
C. The Assert step uses a direct expression instead of the product variable
D. The test method lacks the @Before annotation
Solution
Step 1: Review the Act step
The Act step is the calculation: int product = x * y; which is correct.
Step 2: Check the Assert step
The assertion compares 8 with x * y directly instead of using the product variable, which breaks the pattern clarity.
Final Answer:
The Assert step uses a direct expression instead of the product variable -> Option C
Quick Check:
Assert should check the Act result variable [OK]
Hint: Assert should verify the Act result variable, not repeat calculation [OK]
Common Mistakes:
Thinking Act step is missing
Believing @Before is required for every test
Confusing Arrange with Act
Ignoring the importance of using variables in Assert
5. You want to test a method divide(int a, int b) that throws IllegalArgumentException when b is zero. Using Arrange-Act-Assert, which is the best way to write this test in JUnit 5?
hard
A. Arrange inputs, call divide inside assertThrows, Assert exception is thrown
B. Arrange inputs, call divide normally, Assert result equals zero
C. Call divide without Arrange, Assert no exception thrown
D. Arrange inputs, call divide, Assert result equals a divided by b
Solution
Step 1: Arrange inputs for exception case
Set a to any number and b to zero to trigger the exception.
Step 2: Act by calling divide inside assertThrows
Use assertThrows(IllegalArgumentException.class, () -> divide(a, b)) to check the exception is thrown.
Step 3: Assert the exception is correctly thrown
This confirms the method behaves as expected when dividing by zero.
Final Answer:
Arrange inputs, call divide inside assertThrows, Assert exception is thrown -> Option A
Quick Check:
Use assertThrows for exception tests [OK]
Hint: Use assertThrows to test exceptions in Act-Assert step [OK]
Common Mistakes:
Ignoring exception testing and checking wrong result
Calling method outside assertThrows
Not arranging inputs properly
Asserting wrong expected value
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.