Assertions: assertSame() and assertNotSame()

Understand assertSame() and assertNotSame() methods in JUnit 5.

The assertSame() method

The assertSame() method asserts that two Object variables refer to the same Object.

It has three overloaded methods.

  • If the actual and expected values refer to the same Object, the test case passes.
  • If the actual and expected values don’t refer to the same Object, the test case fails.

Get hands-on with 1400+ tech skills courses.