DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. The Benefit of Using AssertThat Over Other Assert Methods

The Benefit of Using AssertThat Over Other Assert Methods

The latest JUnit4.4 release notes indicate that it's better to use AssertThat over other Assert methods.

Mohammad Nadeem user avatar by
Mohammad Nadeem
·
Mar. 22, 19 · Presentation
Like (6)
Save
Tweet
Share
28.34K Views

Join the DZone community and get the full member experience.

Join For Free

The JUnit4.4 release notes talk about the various benefits of usingassertThat over traditional assertXXX methods, which we will walk through one by one.

assertThat([value], [matcher statement]);


Readability

The new syntax allows you to think in terms of subject, verb, and object (asset that actual is expected) rather than (as in traditional assert statements) verb, object, and subject (assert equals expected actual)

Now, suppose that a variable (actual) should be 100 after a test; here is how one would do that in both versions:

assertEquals(100, actual);
// assertEquals(expected, actual); In general


It is easy to forget the correct order and type it in the reverse order:

assertThat(actual, equalTo(100));
//OR
assertThat(actual, is(equalTo(100)));
//OR
assertThat(actual, is(100));


With this version, there is no confusion; everything is crystal clear. It also reads more like a sentence: “Assert that the actual value is equal to the expected value 100.”

Here is how check of not equals is done in both versions:

assertFalse(expected.equals(actual));


Since there is no assertNotEquals (unless it’s custom coded), we have to use assertFalse and do an equals on the two variables.

assertThat(actual, is(not(equalTo(expected))));
//OR
assertThat(actual, is(not(expected)));


Better/Detailed Failure Messages

assertTrue("Number not between 1 and 3!", 1 < 5 && 5 < 3);
//java.lang.AssertionError: Number not between 1 and 3!


import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;
import static org.junit.Assert.assertThat;


assertThat(5 , allOf(greaterThan(1), lessThan(3)));
//java.lang.AssertionError:
Expected: (a value greater than <1> and a value less than <3>)
     but: a value less than <3> <5> was greater than <3>


Type Safety

assertEquals("abc", 123);
//Compiles but fails


Note that JUnit has a dependency with only hamcrest-core. To take full benefit of matchers, you can use hamcrest-all, and even go beyond and use AssertJ (Fluent Assertion API):

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;


assertThat(123, equalTo("abc"));
//Does not even compiles


AssertJ

Here are some of the examples with AssertJ:

// basic assertions
assertThat(frodo.getName()).isEqualTo("Frodo");
assertThat(frodo).isNotEqualTo(sauron);

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

// collection specific assertions (there are plenty more)
// in the examples below fellowshipOfTheRing is a List<TolkienCharacter>
assertThat(fellowshipOfTheRing).hasSize(9)
                               .contains(frodo, sam)
                               .doesNotContain(sauron);

// as() is used to describe the test and will be shown before the error message
assertThat(frodo.getAge()).as("check %s's age", frodo.getName()).isEqualTo(33);


Note that if the library owner says they prefer this over that, I would switch sooner rather than later; switch does not mean you do it all at once; it just means that, first, you do it the new way for new test cases.

References

  • JUnit 4.4 Release notes
  • Nice article
Type safety Testing Release (agency) JUnit Object (computer science) Dependency Crystal (programming language) Clear (Unix) IT

Published at DZone with permission of Mohammad Nadeem, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Fargate vs. Lambda: The Battle of the Future
  • Spring Boot vs Eclipse MicroProfile: Resident Set Size (RSS) and Time to First Request (TFR) Comparative
  • Cloud Performance Engineering
  • Multi-Cloud Integration

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: