Java Top

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

> CHECK OUT THE COURSE

1. Overview

AssertJ is an assertion library for Java that allows us to write assertions fluently and also makes them more readable.

In this tutorial, we'll explore AssertJ's extracting method to check fluently without breaking the test assertion flow.

2. Implementation

Let's start with a Person example class:

class Person {
    private String firstName;
    private String lastName;
    private Address address;

    Person(String firstName, String lastName, Address address) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.address = address;
    }

    // getters and setter omitted
}

Each Person will be associated with some Address:

class Address {
    private String street;
    private String city;
    private ZipCode zipCode;

    Address(String street, String city, ZipCode zipCode) {
        this.street = street;
        this.city = city;
        this.zipCode = zipCode;
    }

    // getters and setter omitted
}

And each Address will have a ZipCode included as a class:

class ZipCode {
    private long zipcode;

    ZipCode(long zipcode) {
        this.zipcode = zipcode;
    }

    // getters and setter omitted
}

Now let's say after creating a Person object, we need to test the following cases:

  • The Address is not null
  • The Address is not on the list of restricted addresses
  • The ZipCode object is not null
  • The ZipCode value is between 1000 and 100000

3. Common Assertions Using AssertJ

Given the following Person object:

Person person = new Person("aName", "aLastName", new Address("aStreet", "aCity", new ZipCode(90210)));

We can extract the Address object:

Address address = person.getAddress();

Then we can assert that Adress is not null:

assertThat(address).isNotNull();

We can also check that the Address is not in the list of restricted addresses:

assertThat(address).isNotIn(RESTRICTED_ADDRESSES);

The next step is to check the ZipCode:

ZipCode zipCode = address.getZipCode();

And Assert that it is not null:

assertThat(zipCode).isNotNull();

Finally, we can extract ZipCode value and assert that it is between 1000 and 100000:

assertThat(zipCode.getZipcode()).isBetween(1000L, 100_000L);

The above code is straightforward, but we need help reading it fluently as it needs multiple lines to process. We also need to assign variables to be able to assert against them later, which is not a clean code experience.

4. Using AssertJ's Extracting Method

Now let's see how the extracting method can help us:

assertThat(person)
  .extracting(Person::getAddress)
    .isNotNull()
    .isNotIn(RESTRICTED_ADDRESSES)
  .extracting(Address::getZipCode)
    .isNotNull()
  .extracting(ZipCode::getZipcode, as(InstanceOfAssertFactories.LONG))
    .isBetween(1_000L, 100_000L);

As we can see, the code is not very different, but it is fluent and easier to read.

5. Conclusion

In this article, we're able to see two ways to extract an object's value to be asserted on:

  • Extracting into variables to be asserted later on
  • Extracting in a fluent way using AssertJ's extracting method

The examples used in this article can be found over on GitHub.

Java bottom

Get started with Spring 5 and Spring Boot 2, through the Learn Spring course:

>> CHECK OUT THE COURSE
Junit footer banner
2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Comments are closed on this article!