Introduction
We use assertNotEquals to check that two values are different. It helps us make sure our program does not produce unwanted equal results.
We use assertNotEquals to check that two values are different. It helps us make sure our program does not produce unwanted equal results.
assertNotEquals(expected, actual); assertNotEquals(String message, expected, actual);
expected is the value you do NOT want actual to be equal to.
You can add a message to explain the test if it fails.
assertNotEquals(5, calculateSum(2, 2));
assertNotEquals("Values should differ", "hello", getGreeting());
This test checks that newValue is not equal to oldValue. Since 15 is not 10, the test will pass.
import static org.junit.jupiter.api.Assertions.assertNotEquals; import org.junit.jupiter.api.Test; public class SampleTest { @Test public void testNotEquals() { int oldValue = 10; int newValue = 15; assertNotEquals(oldValue, newValue); } }
If the two values are equal, the test will fail and show an error.
Use assertNotEquals to catch unexpected equal results early.
assertNotEquals checks that two values are different.
It helps catch errors where values should not match.
You can add a message to explain the test failure.
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