Search⌘ K
AI Features

assertSame() method

Understand how to use the assertSame() method in JUnit 5 to verify that two references point to the exact same object. This lesson covers different overloaded assertSame methods with examples demonstrating passing and failing test cases, including custom messages for failures.

We'll cover the following...

assertSame() method

Assertions API provide static assertSame() method. This method helps us in validating that expected and actualrefer 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 pass.
  • If the actual and expected value does not refer to the same object then the test case will fail.

There are basically three useful overloaded methods for assertSame:-

Java
public static void assertSame(Object expected, Object actual)
public static void assertSame(Object expected, Object actual, String message)
public static void assertSame(Object expected, Object actual, Supplier<String> messageSupplier)

Demo

...