import static org.junit.jupiter.api.Assertions.assertEquals; int expected = 10; int actual = 5; assertEquals(expected, actual);
Assertions in JUnit compare expected and actual values. If they differ, the test fails with an AssertionError. Here, 5 is not equal to 10, so the assertion fails.
Assertions verify that the program's output matches what we expect. If not, they cause the test to fail, helping us find bugs early.
import static org.junit.jupiter.api.Assertions.assertEquals; String expected = "hello"; String actual = "world"; assertEquals(expected, actual, "Strings do not match!");
When assertEquals fails, it throws an AssertionError showing the provided message to explain the failure.
import java.util.List; import java.util.ArrayList; import static org.junit.jupiter.api.Assertions.assertEquals; List<String> expected = List.of("apple", "banana"); List<String> actual = new ArrayList<>(); actual.add("banana"); actual.add("apple"); assertEquals(expected, actual);
List.equals returns true only if both lists have the same elements in the same order. Here, order differs, so assertion fails.
When an assertion fails, it throws an AssertionError which stops the current test method execution and marks it as failed. Other tests continue normally.
