AssertJ
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
write assertions that read like sentences and give clear failures.
Planned topics
- AssertJ starts with
assertThat(actual)and chains checks. - Collections:
containsExactly,contains,hasSize,extracting. - Exceptions:
assertThatThrownBy(...)orassertThatExceptionOfType(...), then check type and message. - Numbers and decimals:
isEqualByComparingToforBigDecimal. - Failure messages show actual versus expected, so you debug faster.
Draft outline details
Demonstration
assertThat(orders).extracting(Order::status).containsExactly(NEW, PAID);
assertThatThrownBy(() -> cart.add(null))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("product");Check your understanding
Which AssertJ method checks that code throws?
Show answer
assertThatThrownBy.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.