× Home Tutorials Training Consulting Products Books Company Donate Contact us









NOW Hiring

Quick links

Share

AssertJ tutorial. This tutorial describes the usage of the AssertJ framework for writing compact tests.

1. Introduction to AssertJ

The AssertJ project provides fluent assertion statements for Java. These assert statements are typically used with Java JUnit tests. AssertJ is a fork of the Fest assert library, as the Fest is not actively maintained anymore.

AssertJ is a library for simplifying the writing of assert statements in tests. It also improves the readability of asserts statements. It has a fluent interface for assertions. The base method for AssertJ assertions is the assertThat method followed by the assertion.

2. Using AssertJ

2.1. Gradle

To use AssertJ in your Gradle build for unit tests add the following dependency to your Gradle build file.

testCompile 'org.assertj:assertj-core:2.0.0'

2.2. Maven

To use the library for a Maven based project, the following dependency to your pom file.

<dependency>
  <groupId>org.assertj</groupId>
  <artifactId>assertj-core</artifactId>
  <!-- use 3.0.0 for Java 8 projects -->
  <version>2.0.0</version>
  <scope>test</scope>
</dependency>

2.3. Eclipse IDE configuration

To use the AssertJ assertThat statement and not the one from Hamcrest go to Window ▸ Preferences ▸ Java ▸ Editor ▸ Content assist ▸ Favorites ▸ New Type and enter org.assertj.core.api.Assertions.

You should see org.assertj.core.api.Assertions.* in the list.

2.4. IntelliJ Idea configuration

No special configuration, just start typing assertThat and then invoke completion (Ctrl-Space) twice.

3. Example for using AssertJ

The following example code is taken from the AssertJ homepage:

// unique entry point to get access to all assertThat methods and utility methods (e.g. entry)
import static org.assertj.core.api.Assertions.*;

// common assertions
assertThat(frodo.getName()).isEqualTo("Frodo");
assertThat(frodo).isNotEqualTo(sauron)
                 .isIn(fellowshipOfTheRing);

// String specific assertions
assertThat(frodo.getName()).startsWith("Fro")
                           .endsWith("do")
                           .isEqualToIgnoringCase("frodo");

// collection specific assertions
assertThat(fellowshipOfTheRing).hasSize(9)
                               .contains(frodo, sam)
                               .doesNotContain(sauron);

// using extracting magical feature to check fellowshipOfTheRing characters name :)
assertThat(fellowshipOfTheRing)
        .extracting("name")
        .contains("Boromir", "Gandalf", "Frodo", "Legolas")
        .doesNotContain("Sauron", "Elrond");

// map specific assertions, ringBearers initialized with the elves rings and the one ring bearers.
assertThat(ringBearers).hasSize(4)
                       .contains(entry(oneRing, frodo), entry(nenya, galadriel))
                       .doesNotContainEntry(oneRing, aragorn);

// and many more assertions : dates, file, numbers, exceptions ...

4. About this website

5. AssertJ resources

5.1. vogella GmbH training and consulting support

TRAINING SERVICE & SUPPORT

The vogella company provides comprehensive training and education services from experts in the areas of Eclipse RCP, Android, Git, Java, Gradle and Spring. We offer both public and inhouse training. Whichever course you decide to take, you are guaranteed to experience what many before you refer to as “The best IT class I have ever attended”.

The vogella company offers expert consulting services, development support and coaching. Our customers range from Fortune 100 corporations to individual developers.

Copyright © 2012-2016 vogella GmbH. Free use of the software examples is granted under the terms of the EPL License. This tutorial is published under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Germany license.

See Licence.