Test Overview
This test checks if the calculator adds two numbers correctly. It uses a single assertion to verify the sum result.
This test checks if the calculator adds two numbers correctly. It uses a single assertion to verify the sum result.
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class CalculatorTest { @Test public void testAddition() { 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 initialized | — | PASS |
| 2 | Calculator object created | Calculator instance ready | — | PASS |
| 3 | add method called with 2 and 3 | Method returns 5 | — | PASS |
| 4 | assertEquals checks if result equals 5 | Result is 5 | Verify 5 == 5 | PASS |
| 5 | Test ends | Test passed successfully | — | PASS |
sum(2, 3) returns 6?
@Test
void testSum() {
assertEquals(5, sum(2, 3));
}@Test
void testUser() {
assertEquals("John", user.getName());
assertEquals(30, user.getAge());
}calculateDiscount that returns a discount percentage based on user type and purchase amount. How should you write JUnit tests following the single assertion per test debate best practice?Pass the test and this pattern moves unknown