Bird
Raised Fist0
JUnittesting~20 mins

assertNotEquals in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does the JUnit method assertNotEquals check in a test?
easy
A. That two values are not the same
B. That two values are equal
C. That a value is true
D. That a value is null

Solution

  1. Step 1: Understand the purpose of assertNotEquals

    This method is used to verify that two values are different, meaning they should not be equal.
  2. Step 2: Compare with other assertions

    Unlike assertEquals which checks equality, assertNotEquals ensures values differ.
  3. Final Answer:

    That two values are not the same -> Option A
  4. Quick Check:

    assertNotEquals means values differ [OK]
Hint: Remember: 'NotEquals' means values must differ [OK]
Common Mistakes:
  • Confusing assertNotEquals with assertEquals
  • Thinking it checks for null values
  • Assuming it checks boolean true
2. Which of the following is the correct syntax to assert that two integers are not equal in JUnit with a custom error message?
easy
A. assertNotEquals(5, 10);
B. assertNotEquals(5 == 10);
C. assertNotEquals(5, 10, "Error");
D. assertNotEquals(5 != 10);

Solution

  1. Step 1: Review method signatures

    JUnit's assertNotEquals can take two values and an optional message as the last parameter.
  2. Step 2: Check each option

    assertNotEquals(5, 10); is valid but lacks a message; assertNotEquals(5, 10, "Error"); is correct syntax with message; Options B and C are invalid because they misuse boolean expressions.
  3. Final Answer:

    assertNotEquals(5, 10, "Error"); -> Option C
  4. Quick Check:

    Correct syntax includes values and optional message [OK]
Hint: Use assertNotEquals(value1, value2, message) for clarity [OK]
Common Mistakes:
  • Passing boolean expressions instead of values
  • Omitting required parameters
  • Using incorrect parameter order
3. What will happen when the following JUnit test runs?
int a = 3;
int b = 3;
assertNotEquals(a, b);
medium
A. Compilation error due to wrong syntax
B. Test passes because a and b are equal
C. Test passes because a and b are not equal
D. Test fails because a and b are equal

Solution

  1. Step 1: Analyze variable values

    Both a and b are assigned the value 3, so they are equal.
  2. Step 2: Understand assertNotEquals behavior

    assertNotEquals expects the two values to be different. Since they are equal, the assertion fails and the test fails.
  3. Final Answer:

    Test fails because a and b are equal -> Option D
  4. Quick Check:

    Equal values cause assertNotEquals to fail [OK]
Hint: assertNotEquals fails if values are the same [OK]
Common Mistakes:
  • Thinking test passes when values are equal
  • Confusing assertNotEquals with assertEquals
  • Assuming syntax error without checking code
4. Identify the error in this JUnit test snippet:
String expected = "hello";
String actual = "hello";
assertNotEquals(expected, actual);
medium
A. Test fails because expected and actual are equal
B. No error; test passes
C. Syntax error due to missing message
D. NullPointerException at runtime

Solution

  1. Step 1: Check values of variables

    Both expected and actual hold the string "hello", so they are equal.
  2. Step 2: Understand assertNotEquals effect

    The assertion expects different values. Since they are equal, the test fails but no syntax or runtime error occurs.
  3. Final Answer:

    Test fails because expected and actual are equal -> Option A
  4. Quick Check:

    Equal strings cause assertNotEquals to fail [OK]
Hint: assertNotEquals fails if strings match exactly [OK]
Common Mistakes:
  • Assuming test passes when strings are equal
  • Thinking message parameter is mandatory
  • Expecting runtime exceptions incorrectly
5. You want to test that a method generateId() never returns the value 0. Which JUnit assertion using assertNotEquals correctly tests this with 0 as the first argument and a custom message?
hard
A. assertNotEquals(0, 0, "ID should not be zero");
B. assertNotEquals(0, generateId(), "ID should not be zero");
C. assertNotEquals(generateId(), 0);
D. assertNotEquals("0", generateId());

Solution

  1. Step 1: Identify correct parameter order and message

    The expected forbidden value is 0, so it should be the first parameter. The actual value is from generateId(). Adding a message helps explain failure.
  2. Step 2: Evaluate options for correctness

    assertNotEquals(0, generateId(), "ID should not be zero"); correctly uses 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.
  3. Final Answer:

    assertNotEquals(0, generateId(), "ID should not be zero"); -> Option B
  4. Quick Check:

    Use expected value first, actual second, with message [OK]
Hint: Put expected value first, actual second, add message [OK]
Common Mistakes:
  • Swapping expected and actual parameters
  • Comparing different data types
  • Omitting helpful failure message
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard