0
0
JUnittesting~15 mins

assertNotEquals in JUnit - Build an Automation Script

Choose your learning style9 modes available
Verify that two different strings are not equal using assertNotEquals
Preconditions (2)
Step 1: Create two string variables with different values, for example, 'Hello' and 'World'
Step 2: Use assertNotEquals to verify that these two strings are not equal
✅ Expected Result: The test passes because the two strings are different
Automation Requirements - JUnit 5
Assertions Needed:
assertNotEquals to verify two values are not equal
Best Practices:
Use descriptive test method names
Keep tests independent and simple
Use static import for Assertions for cleaner code
Automated Solution
JUnit
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import org.junit.jupiter.api.Test;

public class AssertNotEqualsTest {

    @Test
    void testStringsAreNotEqual() {
        String first = "Hello";
        String second = "World";
        assertNotEquals(first, second, "Strings should not be equal");
    }
}

This test class uses JUnit 5. We import assertNotEquals statically for cleaner code. The test method testStringsAreNotEqual creates two different strings, first and second. Then it calls assertNotEquals to check they are not the same. If they are different, the test passes. The message helps explain the assertion if it fails.

Common Mistakes - 3 Pitfalls
Using assertEquals instead of assertNotEquals
Not importing Assertions statically
Comparing variables with the same value
Bonus Challenge

Now add data-driven testing with 3 different pairs of strings to verify assertNotEquals works for all

Show Hint