Skip to content
Ameen Abozaid
Planned lessonJNR-043-L04

AssertJ

  • Lesson 4 of 6
  • Junior
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(...) or assertThatExceptionOfType(...), then check type and message.
  • Numbers and decimals: isEqualByComparingTo for BigDecimal.
  • Failure messages show actual versus expected, so you debug faster.
Draft outline details

Demonstration

java
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.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.