Fluent assertions for java
Fluent assertions for java
// unique entry point to get access to all assertThat methods and utility methods (e.g. entry) import static org.assertj.core.api.Assertions.*; // in the following examples, fellowshipOfTheRing is a List of TolkienCharacter // basic 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"); // Extracting with Java 8 love assertThat(fellowshipOfTheRing).extracting(character -> character.getRace().getName()) .contains("Hobbit", "Elf") .doesNotContain("Orc"); // filter collection before assertion assertThat(fellowshipOfTheRing).filteredOn("race", HOBBIT) .containsOnly(sam, frodo, pippin, merry); // filter collection with java 8 lambda predicate assertThat(fellowshipOfTheRing).filteredOn(character -> character.getName().contains("o")) .containsOnly(aragorn, frodo, legolas, boromir); // combining filterting and extraction (yes we can) assertThat(fellowshipOfTheRing).filteredOn(character -> character.getName().contains("o")) .containsOnly(aragorn, frodo, legolas, boromir) .extracting(character -> character.getRace().getName()) .contains("Hobbit", "Elf", "Man"); // and many more assertions : map, dates (java 7 and java 8), file, numbers, exceptions ...
AssertJ provides a rich set of assertions, truly-helpful error messages, it improves test code readability and has been designed to be super easy to use within your favorite IDE.
If you are convinced by AssertJ, you can automatically convert your JUnit assertions to AssertJ
You can easily write assertions for your own classes, it will make your test assertions reflect the domain, it's a way to use ubiquitous language in your tests !
We provide an assertion generator to quickly create assertions for your domain model classes.
AssertJ only exists to help the dev community, we listen to users ideas to provide the most useful assertions.
AssertJ is a fork of the great Fest Assert library which is no more active these days.
AssertJ will remain open and free forever.
Provides assertions for Guava types like Multimap, Table, Optional or ByteSource.
Provides assertions for Joda Time like DateTime and LocalDateTime. More will come later, contributions are very welcome !