Bird
Raised Fist0
JUnittesting~20 mins

Arrange-Act-Assert pattern in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Arrange-Act-Assert Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this JUnit test?
Consider the following JUnit test method using the Arrange-Act-Assert pattern. What will be the test result after execution?
JUnit
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class CalculatorTest {
    @Test
    void testAdd() {
        // Arrange
        Calculator calc = new Calculator();
        int a = 5;
        int b = 3;

        // Act
        int result = calc.add(a, b);

        // Assert
        assertEquals(8, result);
    }
}

class Calculator {
    int add(int x, int y) {
        return x + y;
    }
}
ATest fails with NullPointerException
BTest passes successfully
CTest fails with assertion error
DTest fails with compilation error
Attempts:
2 left
💡 Hint
Check the add method logic and the expected assertion value.
❓ assertion
intermediate
1:30remaining
Which assertion correctly follows the Assert step in Arrange-Act-Assert?
You have arranged and acted by calling a method that returns a list of 3 items. Which assertion correctly verifies the list size?
JUnit
List<String> items = List.of("apple", "banana", "cherry");
AassertEquals(3, items.size());
BassertTrue(items == 3);
CassertFalse(items.isEmpty());
DassertNull(items);
Attempts:
2 left
💡 Hint
Check which assertion compares the expected size to the actual size.
🔧 Debug
advanced
2:30remaining
Identify the problem in this Arrange-Act-Assert test method
This JUnit test method is intended to test subtraction but fails unexpectedly. What is the main issue?
JUnit
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

class CalculatorTest {
    @Test
    void testSubtract() {
        // Arrange
        Calculator calc = new Calculator();
        int a = 10;
        int b = 4;

        // Act
        int result = calc.add(a, b);

        // Assert
        assertEquals(6, result);
    }
}

class Calculator {
    int add(int x, int y) {
        return x + y;
    }
}
AThe Assert step expects wrong result value
BThe Calculator class is missing the subtract method
CThe Arrange step initializes variables incorrectly
DThe Act step calls add instead of subtract method
Attempts:
2 left
💡 Hint
Check which method is called in the Act step versus what the test intends.
🧠 Conceptual
advanced
1:30remaining
Why is the Arrange-Act-Assert pattern important in unit testing?
Choose the best explanation for why the Arrange-Act-Assert pattern is used in writing unit tests.
AIt automatically generates test data and expected results
BIt forces tests to run faster by minimizing code
CIt organizes tests clearly into setup, execution, and verification steps, improving readability and maintenance
DIt allows tests to run in parallel without conflicts
Attempts:
2 left
💡 Hint
Think about how clear structure helps when reading or fixing tests later.
❓ framework
expert
1:30remaining
In JUnit 5, which annotation best supports the Arrange-Act-Assert pattern for repeated setup before each test?
You want to run setup code before each test method to prepare test data (Arrange step). Which annotation should you use?
A@BeforeEach
B@BeforeAll
C@TestInstance
D@AfterEach
Attempts:
2 left
💡 Hint
Consider which annotation runs before every test method individually.

Practice

(1/5)
1. What is the main purpose of the Arrange-Act-Assert pattern in JUnit testing?
easy
A. To write tests without any setup
B. To organize tests into setup, action, and verification steps
C. To skip assertions in tests
D. To run tests in parallel automatically

Solution

  1. Step 1: Understand the pattern components

    Arrange-Act-Assert divides a test into three clear parts: setup (Arrange), execution (Act), and checking results (Assert).
  2. 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.
  3. Final Answer:

    To organize tests into setup, action, and verification steps -> Option B
  4. Quick Check:

    Arrange-Act-Assert = organize test steps [OK]
Hint: Remember: Arrange = setup, Act = do, Assert = check [OK]
Common Mistakes:
  • Thinking Arrange means skipping setup
  • Believing Act is for assertions
  • Confusing Assert with running tests
  • Assuming it runs tests in parallel
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

  1. Step 1: Recall the pattern sequence

    The pattern always starts with Arrange (setup), then Act (execute), and finally Assert (verify).
  2. Step 2: Match the correct order

    Only Arrange, Act, Assert lists the steps in the correct order: Arrange, Act, Assert.
  3. Final Answer:

    Arrange, Act, Assert -> Option A
  4. 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

  1. Step 1: Identify the code purpose

    The line calls the method under test to perform the action, storing the output.
  2. Step 2: Match to pattern step

    This is the 'Act' step where the test executes the behavior to be tested.
  3. Final Answer:

    Act -> Option D
  4. 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

  1. Step 1: Review the Act step

    The Act step is the calculation: int product = x * y; which is correct.
  2. 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.
  3. Final Answer:

    The Assert step uses a direct expression instead of the product variable -> Option C
  4. 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

  1. Step 1: Arrange inputs for exception case

    Set a to any number and b to zero to trigger the exception.
  2. Step 2: Act by calling divide inside assertThrows

    Use assertThrows(IllegalArgumentException.class, () -> divide(a, b)) to check the exception is thrown.
  3. Step 3: Assert the exception is correctly thrown

    This confirms the method behaves as expected when dividing by zero.
  4. Final Answer:

    Arrange inputs, call divide inside assertThrows, Assert exception is thrown -> Option A
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard