VOOZH about

URL: https://www.javacodegeeks.com/2018/01/write-custom-assertj-assertions.html

⇱ Write custom AssertJ assertions - Java Code Geeks


AssertJ is an alternative matching library to the widely used Hamcrest matchers. For my own projects I in fact have changed to solely use AssertJ — I just find the fluid interfaces and extensibility quite appealing.

You can write custom assertions as follows:

Imagine a coffee with a strength and a drink type, such as Espresso or Latte. A custom CoffeeAssert validates coffee instances based on their custom business logic — in this case their properties.

public class CoffeeAssert extends AbstractAssert<CoffeeAssert, Coffee> {

 public CoffeeAssert(Coffee actual) {
 super(actual, CoffeeAssert.class);
 }

 public static CoffeeAssert assertThat(Coffee actual) {
 return new CoffeeAssert(actual);
 }

 public CoffeeAssert hasType(Coffee.Type type) {
 isNotNull();

 if (actual.getType() != type) {
 failWithMessage("Expected the coffee type to be <%s> but was <%s>", type, actual.getType());
 }

 return this;
 }

 // hasStrength(Strength) omitted ...

 public CoffeeAssert isNotDecaf() {
 isNotNull();

 if (actual.getStrength() == Coffee.Strength.DECAF) {
 failWithMessage("Expected a coffee but got decaf!");
 }

 return this;
 }
}

Coffee instances can then simply be validated using the custom assertion. The static import of the assertThat has to refer to CoffeeAssert.

import static com.example.coffee.CoffeeAssert.assertThat;
...

Coffee coffee = new Coffee();
coffee.setStrength(Strength.STRONG);
coffee.setType(Type.ESPRESSO);

assertThat(coffee)
 .hasType(Type.ESPRESSO)
 .isNotDecaf();

The use of custom assertions can vastly improve the quality of your test code.

This post was reposted from my newsletter issue 012

Found the post useful? Subscribe to my newsletter for more free content, tips and tricks on IT & Java:

Published on Java Code Geeks with permission by Sebastian Daschner, partner at our JCG program. See the original article here: Write custom AssertJ assertions

Opinions expressed by Java Code Geeks contributors are their own.

Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

Thank you!

We will contact you soon.

👁 Photo of Sebastian Daschner
Sebastian Daschner
January 2nd, 2018Last Updated: January 2nd, 2018
0 126 1 minute read

Sebastian Daschner

Sebastian Daschner is a self-employed Java consultant and trainer. He is the author of the book 'Architecting Modern Java EE Applications'. Sebastian is a Java Champion, Oracle Developer Champion and JavaOne Rockstar.
Subscribe

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button
Close
wpDiscuz