Want to stop guessing if your tests really check what they should? Discover Arrange-Act-Assert!
Why Arrange-Act-Assert pattern in JUnit? - Purpose & Use Cases
Imagine testing a calculator app by clicking buttons and writing down results on paper every time you want to check if addition works.
This manual way is slow and easy to mess up. You might forget steps or write wrong results, making it hard to trust your tests.
The Arrange-Act-Assert pattern organizes tests clearly: first set up data (Arrange), then do the action (Act), and finally check the result (Assert). This makes tests easy to read and reliable.
Calculator calc = new Calculator(); int result = calc.add(2, 3); if(result == 5) { System.out.println("Test passed"); } else { System.out.println("Test failed"); }
@Test
void testAddition() {
// Arrange
Calculator calc = new Calculator();
// Act
int result = calc.add(2, 3);
// Assert
assertEquals(5, result);
}This pattern makes writing and understanding tests simple, so you can catch bugs faster and keep your code safe.
When building a shopping cart, you can arrange items, act by adding them to the cart, and assert the total price is correct, all clearly separated.
Arrange-Act-Assert breaks tests into clear steps.
It reduces mistakes and confusion in testing.
Helps write reliable and easy-to-read tests.
Practice
Solution
Step 1: Understand the pattern components
Arrange-Act-Assert divides a test into three clear parts: setup (Arrange), execution (Act), and checking results (Assert).Step 2: Identify the purpose
This separation helps make tests easier to read and maintain by clearly showing what is prepared, what is done, and what is checked.Final Answer:
To organize tests into setup, action, and verification steps -> Option BQuick Check:
Arrange-Act-Assert = organize test steps [OK]
- Thinking Arrange means skipping setup
- Believing Act is for assertions
- Confusing Assert with running tests
- Assuming it runs tests in parallel
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 AQuick Check:
Order = Arrange, Act, Assert [OK]
- Mixing up the order of Act and Assert
- Starting with Assert before setup
- Skipping the Arrange step
- Confusing Act with Assert
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?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 DQuick Check:
Calling method = Act step [OK]
- Confusing Act with Arrange (setup)
- Thinking assertEquals line is Act
- Assuming variable declarations are Act
- Selecting Cleanup which is not part of AAA
@Test
void testMultiply() {
int x = 4;
int y = 2;
int product = x * y;
assertEquals(8, x * y);
}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 withx * ydirectly instead of using theproductvariable, which breaks the pattern clarity.Final Answer:
The Assert step uses a direct expression instead of the product variable -> Option CQuick Check:
Assert should check the Act result variable [OK]
- Thinking Act step is missing
- Believing @Before is required for every test
- Confusing Arrange with Act
- Ignoring the importance of using variables in Assert
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?Solution
Step 1: Arrange inputs for exception case
Setato any number andbto zero to trigger the exception.Step 2: Act by calling divide inside assertThrows
UseassertThrows(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 AQuick Check:
Use assertThrows for exception tests [OK]
- Ignoring exception testing and checking wrong result
- Calling method outside assertThrows
- Not arranging inputs properly
- Asserting wrong expected value
Ready to prove it?
Pass the test and this pattern moves unknown
