Test Overview
This test checks if the add method of a simple calculator returns the correct sum of two numbers using assertEquals.
This test checks if the add method of a simple calculator returns the correct sum of two numbers using assertEquals.
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class CalculatorTest { @Test public void testAdd() { Calculator calc = new Calculator(); int result = calc.add(2, 3); 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 | — | PASS |
| 2 | Calculator object is created | Calculator instance ready for use | — | PASS |
| 3 | Calls add(2, 3) method | Method executes and returns 5 | — | PASS |
| 4 | assertEquals checks if expected 5 equals actual 5 | Comparison of expected and actual values | assertEquals(5, 5) | PASS |
| 5 | Test finishes successfully | Test marked as passed in report | — | PASS |
assertEquals in JUnit testing?assertEquals with a message in JUnit?int expected = 5;
int actual = 3 + 2;
assertEquals(expected, actual);
assertEquals(5, "5");
assertEquals usage **including a custom error message** correctly checks if the returned list matches List.of("a", "b")?List<String> expected = List.of("a", "b");
List<String> actual = myMethod();Pass the test and this pattern moves unknown