AssertJ assertions generator

The Assertion Generator is able to create assertions specific to your own classes, it comes with a simple command line tool and a maven plugin.

Having the possibility to create assertions specific to your own classes is important because it makes your test assertions reflect the domain model. It's a way to use Domain Driven Design ubiquitous language in your tests. Custom assertions can be seen as a DSL of domain assertions.

Let's say that you have a Player class with name and team properties, the generator is able to create a PlayerAssert assertions class with hasName and hasTeam assertions. Use it to write code like :

assertThat(mvp).hasName("Lebron James").hasTeam("Miami Heat");

The generator will also create entry point classes like Assertions, BddAssertions and SoftAssertions/JUnitSoftAssertions to ease accessing each generated *Assert classes. In the case where PlayerAssert and GameAssert have been generated, the generated Assertions class will look like:

public class Assertions {

  /**
   * Creates a new instance of <code>{@link GameAssert}</code>.
   *
   * @param actual the actual value.
   * @return the created assertion object.
   */
  public static GameAssert assertThat(Game actual) {
    return new GameAssert(actual);
  }

  /**
   * Creates a new instance of <code>{@link PlayerAssert}</code>.
   *
   * @param actual the actual value.
   * @return the created assertion object.
   */
  public static PlayerAssert assertThat(Player actual) {
    return new PlayerAssert(actual);
  }

  /**
   * Creates a new <code>{@link Assertions}</code>.
   */
  protected Assertions() {
    // empty
  }
}

This quickstart guide is for non maven users, if you are a maven user, you should use the assertions generator maven plugin.

Get the AssertJ Assertions Generator archive (either for unix or windows) and extract it, its structure is :

assertion-generator/
  |-- lib/
  |-- templates/
  |-- generate-assertions script (.bat or .sh)

First, put in lib/ the jars containing the classes you want to generate assertions for, then run the script with either the classes or the packages containing the classes you want to generate assertions for.

Example : generating assertions for TolkienCharacter class :

generate-assertions.sh org.assertj.examples.data.TolkienCharacter

Example : generating assertions for all classes in org.assertj.examples.data :

generate-assertions.sh org.assertj.examples.data

Your assertions classes have been generated ! You can find them in the current directory.

Tip :

You can change what is generated by modifying the templates used for assertions creation (have a look at templates directory)

Release date : 2014-11-02

  • Generate JUnitSoftAssertions.
  • Fix potential import clash by using fully qualified class names (except for AssertJ classes).
  • Fix class cast exception when an overriden getter returns a generic type.

Release date : 2014-08-31

  • Fixes bad generated code in hasNoXXX assertion for iterable property.
  • Fixes bad generated code in contains assertion for iterable property with upper bound wildcard (? extends).
  • Fix to generate assertion for classes having '$' character in class name.
  • Fix fenerated assertion for public boolean field.
  • Use fully qualified name class instead of import in Assertions entry point classes.

Release date : 2014-08-17

  • Correctly resolve generic return types.
  • Use Guava ClassPath instead of org.reflections.

Release date : 2014-08-03

Big thanks to Fr Jeremy Krieg for his work on this release.

  • Generate assertions using offset for double and float types.
  • Allow to generate assertion classes that don't hide subclasses assertions after using an assertion from a super class. (Fr Jeremy Krieg)
  • Generate assertions for public fields.
  • Prepend enclosing class name to inner class assertion name to avoid clash when several inner classes have the same name.
  • Don't generate classes with unused import. (Fr Jeremy Krieg)
  • Generated javadoc improvements. (Fr Jeremy Krieg)
  • Allow to specify where to generate Assertions entry point classes. (floppywaste)
  • Generate a BDD style entry point class BddAssertions replacing assertThat methods by then.
  • Generate a SoftAssertions entry point class to collect all assertion errors instead of stopping at the first one.

Release date : 2013-12-08

  • To ease using generated assertions, generate an entry point class Assertions providing assertThat methods giving access to each generated *Assert classes.
  • Don't generate hasDeclaringClass assertion for Enum.
  • In generated method assertions, rename errorMessage local variable to assertjErrorMessage to avoid collision when object under test has an errorMessage property. (William Delanoue)

Release date : 2013-09-15

Big thanks to Fabien Duminy, he has done most of the work of this release !

  • Generated assertions use null safe equals for non primtive types.
  • Add exceptions to assertion method signature if underlying getter of class to assert throws some. (Fabien Duminy)
  • Generate assertions for nested/inner classes. (Fabien Duminy)
  • Fix error message displaying unrelevant property value when getter of class to assert doesn't return the same value on successive calls. (Fabien Duminy)

Release date : 2013-03-26

It's the first release after Fest fork, generated assertions are cleaner.

If you have any questions, please use AssertJ google group.

AssertJ assertions generator is hosted on github : https://github.com/joel-costigliola/assertj-assertions-generator.

Please report bugs or missing features in AssertJ assertions generator issue tracker.

Thanks for your interest ! Please check our contributor's guidelines.

Special thanks to AssertJ assertions generator contributors:

  • Fabien Duminy
  • William Delanoue
  • floppywaste
  • Fr Jeremy Krieg