0
0
JUnittesting~20 mins

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

Choose your learning style9 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.