A project demonstrating the differences between popular assertion libraries in Java
This compares four assertion libraries:
- JUnit Assert
- Hamcrest
- Google Truth
- assertj
Acknowledging that GitHub is not the be-all, end-all exhaustive authority on what projects use these assertions, it is, nevertheless, sufficient for the needs of sampling popularity of these various assertion libraries.
The following statistics exist for declaration of dependencies on each assertion library:
- JUnit Assert: unfortunately, given the nature of how these assertions are embedded into the junit.jar file itself, it is not possible to distinguish between POMs that depend on JUnit for merely the ability to execute JUnit tests and those who depend on it for its assertion functionality.
- Hamcrest: using the hamcrest-core library as the determination of yields, at time of writing this, 33,380 results for POMs and 5,393 for Gradle, for a total of 38,773. This may be a small estimation, however, as some projects, such as mockito-all bundle Hamcrest classes in with their assemblies, so there may be false negatives in projects that use libraries like mockito-all to bring in their own functionality as well as Hamcrest functionality.
- Google Truth: using the com.google.truth group ID (since the artifact ID of "truth" is likely to result in a non-negligible amount of false positives) yields, at time of writing this, 2,535 results for POMs and 1,437 results for Gradle, for a total of 3,972.
- assertj: using the assertj-core library as the determination of yields, at time of writing this, 30,277 results for POMs and 9,090 results for Gradle, for a total of 39,367.
The following statistics exist for usage of these assertion libraries in Java code, searching by import statements.
- JUnit Assert: searching for imports of org.junit.Assert yields 3,176,406 results
- Hamcrest: searching for imports of org.hamcrest.Matchers yields 396,818 results
- Google Truth: searching for imports of com.google.common.truth yields 57,340 results
- assertj: searching for imports of org.assertj.core.api yields 303,846 results