0
0
JUnittesting~20 mins

assertNotEquals in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
assertNotEquals Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
assertion
intermediate
2: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);
    }
}
AThe test will pass because 5 does not equal 3 + 2
BThe test will fail because 5 equals 3 + 2
CThe test will fail due to a compilation error
DThe test will pass because assertNotEquals always passes
Attempts:
2 left
💡 Hint
Remember assertNotEquals expects the two values to be different for the test to pass.
assertion
intermediate
2: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);
    }
}
AThe test will fail because assertNotEquals cannot compare objects
BThe test will pass because assertNotEquals compares references
CThe test will fail because a and b have the same content
DThe test will pass because a and b are different objects
Attempts:
2 left
💡 Hint
assertNotEquals uses equals() method to compare objects, not references.
Predict Output
advanced
2: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);
    }
}
AThe test will fail due to a NullPointerException
BThe test will fail because arr1 and arr2 have the same contents
CThe test will pass because assertNotEquals compares array contents
DThe test will pass because arr1 and arr2 are different array objects
Attempts:
2 left
💡 Hint
Arrays do not override equals(), so equals() compares references.
🔧 Debug
advanced
2: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);
  }
}
APerson class does not override equals(), so default reference equality is used
BPerson class overrides equals() incorrectly causing failure
CThe test fails because p1 and p2 have the same name
DassertNotEquals cannot compare custom objects
Attempts:
2 left
💡 Hint
Check if equals() is overridden correctly to compare fields.
🧠 Conceptual
expert
2:00remaining
Choosing assertNotEquals vs assertEquals in test design
Which scenario best justifies using assertNotEquals instead of assertEquals in a test case?
AVerifying that a method changes a value from its initial state
BChecking that two objects have identical content
CConfirming that a method returns a specific expected value
DEnsuring that two variables reference the exact same object
Attempts:
2 left
💡 Hint
assertNotEquals is used to confirm values are different, not equal.