How To Improve Your Assertions Using AssertJ

Overview:

As you might already know,Fluent APIs make your code readable and easily maintainable. We already have seen few articles on designing Page Objects and Business Workflows in fluent style. In this article, Lets see how we could include fluent assert statements for your automated tests using AssertJ library. Take a look at the examples in this articles. You could easily compare that with your JUnit/TestNG assert statements and understand the benefits of using AssertJ.

AssertJ:

AssertJ is a simple assertion library for Java written in fluent style. It almost covers the assertions for all the possible data types. It also lets you to extend the library for your custom objects. It also lets you convert your existing JUnit/TestNG assert statements into AssertJ assert statements by using a shell script which is cool.

Maven:

To include AssertJ in your Java project, include below maven dependency in your pom file. Check the maven repo here for the latest versions.

 

<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>3.9.1</version>
    <scope>test</scope>
</dependency>

 

Examples:

Lets start with few simple examples.

  • boolean check
boolean actual = false;

        assertThat(actual).isTrue();
//or
        assertThat(actual).isEqualTo(true);
  • Sting comparison.
@Test
public void stringCompare(){

    String expected = "Test Automation Guru";
    String actual   = "test automation guru";

    assertThat(actual).isEqualTo(expected);

}

Running the above code produces below output.

java.lang.AssertionError: 
Expecting:
 <"test automation guru">
to be equal to:
 <"Test Automation Guru">
but was not.

You can also include some meaningful description for your assert statements.

assertThat(actual).as("AssertJ String Comparison Check").isEqualTo(expected);
  • Soft Assertions. You might already know what it means. In case, if you are not aware, The above assertions we had seen so far is hard assertion. When you have more than 1 assert statements, as soon as an assert statement fails, the remaining assert statements do not get executed. This behvaior is same for all AssertJ/JUnit and TestNG libraries. Soft Assertions on the other hand execute all the assert statements and provide the result of all assertions.
String expected = "Test";
String actual = "Test";

SoftAssertions.assertSoftly(s -> {
    s.assertThat(actual).doesNotContain("t");
    s.assertThat(actual).doesNotStartWith("Test");
    s.assertThat(actual).doesNotEndWith("her");
    s.assertThat(actual).isEqualTo(expected);
});

Summary:

AssertJ is one of the coolest libraries we have in Java. It makes your test automation script well readable and easily maintainable by chaining various assertions. If you have already implemented all your assertions using JUnit/TestNg, No worries! AssertJ provides an utility to convert all your assertions to assertj assertions.

Lets write better code and have a robust test script!

 

Happy Testing & Subscribe 🙂

Add a Comment

Your email address will not be published. Required fields are marked *