Test Overview
This test checks if the addition of two numbers works correctly using JUnit. It verifies that 2 + 3 equals 5.
This test checks if the addition of two numbers works correctly using JUnit. It verifies that 2 + 3 equals 5.
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class CalculatorTest { @Test public void testAddition() { int result = 2 + 3; assertEquals(5, result, "2 + 3 should equal 5"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | JUnit test runner initializes the CalculatorTest class | — | PASS |
| 2 | Runs testAddition method | Inside testAddition method, calculation 2 + 3 is performed | — | PASS |
| 3 | Calls assertEquals to check if result equals 5 | assertEquals compares expected value 5 with actual result 5 | assertEquals(5, result) verifies 5 == 5 | PASS |
| 4 | Test completes successfully | Test runner marks testAddition as passed | — | PASS |
@Test?@Test annotation@Test annotation marks a method as a test method in JUnit, meaning it will be run to check code behavior.@Test marks test methods [OK]@Test annotation to mark a method as a test.assertEquals(expected, actual). @Test public void testMethod() { assertEquals(5, 2 + 3); } matches this.import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class SampleTest {
@Test
void additionTest() {
assertEquals(10, 5 + 5);
}
}import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CalcTest {
@Test
public void testSum() {
int result = 2 + 2;
assertEquals(5, result);
}
}multiply(int a, int b) returns 0 when one input is 0. Which test method is correct?@Test and return void. @Test public void testMultiplyZero() { assertEquals(0, multiply(0, 5)); } meets this.assertEquals(0, multiply(0, 5)), which is correct.Pass the test and this pattern moves unknown