Fluent tests in Java with AssertJ

Dariusz MydlarzDariusz Mydlarz
1 min read

I haven't been using them extensively, but recently I started to take advantage of the AssertJ library.

I want to draw your attention to how easy and pleasant it is to write and read tests written in AssertJ.

Just take a look at the tests I wrote for a collection.

@Test
void returnsUserByCompany() {
    // given
    var users = givenUsers(
        new User("Mark", "Facebook"),
        new User("Eduardo", "Facebook"),
        new User("Elon", "X"),
    )

    // when
    List<User> user = getUserByCompany("Facebook");

    // then
    assertThat(fields).extracting(User::getName)
        .contains("Mark", "Eduardo")
        .doesNotContain("Elon");
}

For more information, take a look at the library docs: https://assertj.github.io/doc/

0
Subscribe to my newsletter

Read articles from Dariusz Mydlarz directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dariusz Mydlarz
Dariusz Mydlarz