Test Overview
This test checks if the calculator adds two numbers correctly. It shows how a clear test structure helps understand what is tested and what is expected.
This test checks if the calculator adds two numbers correctly. It shows how a clear test structure helps understand what is tested and what is expected.
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class CalculatorTest { @Test void testAddition() { // Arrange: create calculator instance Calculator calc = new Calculator(); // Act: add two numbers int result = calc.add(2, 3); // Assert: verify the result is correct assertEquals(5, result, "2 + 3 should equal 5"); } } class Calculator { public int add(int a, int b) { return a + b; } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | JUnit test runner initializes CalculatorTest class | - | PASS |
| 2 | Creates Calculator instance | Calculator object ready for use | - | PASS |
| 3 | Calls add method with 2 and 3 | Calculator processes addition | - | PASS |
| 4 | Checks if result equals 5 using assertEquals | Test verifies output matches expected value | assertEquals(5, result) | PASS |
| 5 | Test ends successfully | Test runner reports test passed | - | PASS |