JUnit 5 Fundamentals
Checking for equality with assertEquals() & Friends
Up next
Previous
About
Overview
In this lesson, we'll highlight the importance of assertions for any good testing framework, and introduce the first set of assertion methods provided by JUnit Jupiter. Using assertEquals()
and assertSame()
, a computational result can be compared against an expected value, and fail its test in case of a mismatch. While assertEquals()
compares objects based on their equals()
implementation, assertSame()
checks references through the ==
operator. We'll explore a test comparing two objects that fails with assertSame()
, but passes when changing to assertEquals()
.
Furthermore, assertEquals()
has various sibling methods, which we'll discover afterwards. These assertions provide very convenient access to checking equality inside collections, arrays, as well as inequality.
Summary
- Your favorite assertion framework still works with JUnit 5
assertEquals()
compares objectsassertSame()
compares object referencesassertArrayEquals()
checks each element of two arraysassertIterableEquals()
checks each element of two iterable collectionsassertNotEquals()
andassertNotSame()
are the inverse toassertEquals()
andassertSame()
- Assertions can be given a message for better logging
Instructor
Links
Comments
Lessons in JUnit 5 Fundamentals






































