How To Improve Your Assertions Using AssertJ


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


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





Lets start with few simple examples.

  • boolean check
boolean actual = false;

  • Sting comparison.
public void stringCompare(){

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



Running the above code produces below output.

 <"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 -> {


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 🙂