Test Overview
This test checks if the Calculator class correctly adds two numbers using the Arrange-Act-Assert pattern. It verifies that the sum of 5 and 3 equals 8.
This test checks if the Calculator class correctly adds two numbers using the Arrange-Act-Assert pattern. It verifies that the sum of 5 and 3 equals 8.
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class CalculatorTest { @Test public void testAdd() { // Arrange Calculator calculator = new Calculator(); int a = 5; int b = 3; // Act int result = calculator.add(a, b); // Assert assertEquals(8, result, "Sum should be 8"); } } class Calculator { public int add(int x, int y) { return x + y; } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | JUnit test runner initializes CalculatorTest class | - | PASS |
| 2 | Arrange: Create Calculator instance and set inputs a=5, b=3 | Calculator object created, variables a=5, b=3 assigned | - | PASS |
| 3 | Act: Call add method with a and b | Calculator.add(5, 3) called, returns 8 | - | PASS |
| 4 | Assert: Check if result equals 8 | Result is 8 | assertEquals(8, result) | PASS |
| 5 | Test ends successfully | Test passed with no errors | - | PASS |