Challenge - 5 Problems
assertNotEquals Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ assertion
intermediate2:00remaining
Understanding assertNotEquals behavior
What will happen when the following JUnit assertion runs?
assertNotEquals(5, 3 + 2);
JUnit
import static org.junit.jupiter.api.Assertions.assertNotEquals; public class TestClass { @org.junit.jupiter.api.Test void testValues() { assertNotEquals(5, 3 + 2); } }
Attempts:
2 left
💡 Hint
Remember assertNotEquals expects the two values to be different for the test to pass.
✗ Incorrect
assertNotEquals fails the test if the two values are equal. Since 3 + 2 equals 5, the assertion fails.
❓ assertion
intermediate2:00remaining
Using assertNotEquals with objects
Given the following code, what is the result of the assertion?
String a = new String("test");
String b = new String("test");
assertNotEquals(a, b);JUnit
import static org.junit.jupiter.api.Assertions.assertNotEquals; public class TestClass { @org.junit.jupiter.api.Test void testStrings() { String a = new String("test"); String b = new String("test"); assertNotEquals(a, b); } }
Attempts:
2 left
💡 Hint
assertNotEquals uses equals() method to compare objects, not references.
✗ Incorrect
assertNotEquals compares objects using their equals() method. Since both strings have the same content, equals() returns true, so the assertion fails.
❓ Predict Output
advanced2:00remaining
Output of assertNotEquals with arrays
What happens when this test runs?
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
assertNotEquals(arr1, arr2);JUnit
import static org.junit.jupiter.api.Assertions.assertNotEquals; public class TestClass { @org.junit.jupiter.api.Test void testArrays() { int[] arr1 = {1, 2, 3}; int[] arr2 = {1, 2, 3}; assertNotEquals(arr1, arr2); } }
Attempts:
2 left
💡 Hint
Arrays do not override equals(), so equals() compares references.
✗ Incorrect
assertNotEquals uses equals() which for arrays compares references. Since arr1 and arr2 are different objects, the assertion passes.
🔧 Debug
advanced2:00remaining
Debugging assertNotEquals failure with custom objects
A test fails with assertNotEquals on two custom objects even though their fields differ. What is the likely cause?
class Person {
String name;
Person(String name) { this.name = name; }
}
Person p1 = new Person("Alice");
Person p2 = new Person("Bob");
assertNotEquals(p1, p2);JUnit
import static org.junit.jupiter.api.Assertions.assertNotEquals; class Person { String name; Person(String name) { this.name = name; } } public class TestClass { @org.junit.jupiter.api.Test void testPersons() { Person p1 = new Person("Alice"); Person p2 = new Person("Bob"); assertNotEquals(p1, p2); } }
Attempts:
2 left
💡 Hint
Check if equals() is overridden correctly to compare fields.
✗ Incorrect
The test fails (meaning equals() returns true) despite different fields, so the likely cause is that Person overrides equals() incorrectly, not properly comparing the fields.
🧠 Conceptual
expert2:00remaining
Choosing assertNotEquals vs assertEquals in test design
Which scenario best justifies using assertNotEquals instead of assertEquals in a test case?
Attempts:
2 left
💡 Hint
assertNotEquals is used to confirm values are different, not equal.
✗ Incorrect
assertNotEquals is useful to check that a value has changed from an initial state, ensuring the method under test modifies data.