Test Overview
This test checks that two strings are not equal using assertNotEquals in JUnit. It verifies that the actual output differs from an incorrect expected value.
This test checks that two strings are not equal using assertNotEquals in JUnit. It verifies that the actual output differs from an incorrect expected value.
import static org.junit.jupiter.api.Assertions.assertNotEquals; import org.junit.jupiter.api.Test; public class NotEqualsTest { @Test public void testStringsAreNotEqual() { String actual = "Hello World"; String wrongExpected = "Hello Jupiter"; assertNotEquals(wrongExpected, actual, "Strings should not be equal"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | JUnit test runner initialized | — | PASS |
| 2 | Runs testStringsAreNotEqual method | Method execution begins | — | PASS |
| 3 | Assigns actual = "Hello World" | Variable actual holds "Hello World" | — | PASS |
| 4 | Assigns wrongExpected = "Hello Jupiter" | Variable wrongExpected holds "Hello Jupiter" | — | PASS |
| 5 | Calls assertNotEquals(wrongExpected, actual, "Strings should not be equal") | Comparing "Hello Jupiter" and "Hello World" | Verify that wrongExpected != actual | PASS |
| 6 | Test completes successfully | No exceptions thrown | — | PASS |
assertNotEquals check in a test?assertNotEqualsassertEquals which checks equality, assertNotEquals ensures values differ.assertNotEquals means values differ [OK]assertNotEquals can take two values and an optional message as the last parameter.int a = 3;
int b = 3;
assertNotEquals(a, b);
a and b are assigned the value 3, so they are equal.assertNotEquals expects the two values to be different. Since they are equal, the assertion fails and the test fails.String expected = "hello";
String actual = "hello";
assertNotEquals(expected, actual);
expected and actual hold the string "hello", so they are equal.generateId() never returns the value 0. Which JUnit assertion using assertNotEquals correctly tests this with 0 as the first argument and a custom message?generateId(). Adding a message helps explain failure.assertNotEquals(0, generateId(), "ID should not be zero"). assertNotEquals(generateId(), 0); misses the message and reverses parameters. assertNotEquals(0, 0, "ID should not be zero"); compares 0 to 0, which always fails. assertNotEquals("0", generateId()); compares string "0" to int, which is incorrect.Pass the test and this pattern moves unknown