Search⌘ K
AI Features

assertNotSame() method

Explore the assertNotSame() method in JUnit 5 to ensure two references do not point to the same object. Understand how to apply its overloaded versions with examples and error messages to write precise unit tests. This lesson helps you verify object identity effectively in Java testing.

We'll cover the following...

assertNotSame() method

Assertions API provide static assertNotSame() method. This method helps us in validating that expected and actual do not refer to the exact same object. JUnit uses == operator to perform this assert.

  • If the actual and expected value refers to the same object then the test case will fail.
  • If the actual and expected value does not refer to the same object then the test case will pass.

There are basically three useful overloaded methods for assertNotSame:-

Java
public static void assertNotSame(Object actual)
public static void assertNotSame(Object actual, String message)
public static void assertNotSame(Object actual, Supplier<> messageSupplier)
...