DZone
Java Zone
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
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Using Hamcrest and JUnit

Using Hamcrest and JUnit

Rafael Naufal user avatar by
Rafael Naufal
·
Apr. 05, 10 · Java Zone · Interview
Like (0)
Save
Tweet
36.58K Views

Join the DZone community and get the full member experience.

Join For Free

Lately I started using the core Hamcrest matchers bundled with the JUnit framework to create more readable unit tests.

Hamcrest matchers were created to improve the readability of unit testing code. It’s a framework which facilitates the creation of matcher objects to match rules specified in unit tests. Some examples will let it to be clearer:

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

@Test
public void shouldBeTheSamePerson()
{
Person me = new Person( "Rafael" );
Person theOther = new Person( "Rafael" );
assertThat( me, is( theOther ) );
}

@Test
public void shouldHaveFixedSizeNumbers()
{
List<Integer> numbers = Arrays.asList( 1, 2, 3, 4, 5 );
assertThat( numbers.size(), is( equalTo( 5 ) ) );
}

The first example checks if one Person object is equal to another using the Object equals method, which was overridden in the Person class. The is syntax defines a matcher which is a shorthand to is(equalTo(value)). The second one uses the is(equalTo(value)) matcher to check the size of an integer list of fixed size numbers. The assertThat method is used in conjunction with the is(equalTo(value)) matcher, which makes the test sentence very human readable.

An interesting thing is the possibility to create a custom matcher, like this one which tests if a given list only has even numbers:

public class AreEvenNumbers extends TypeSafeMatcher<Collection<Integer>> {

@Override
public boolean matchesSafely(Collection<Integer> numbers) {
for (Integer number : numbers) {
if (number % 2 != 0) {
return false;
}
}
return true;
}

@Override
public void describeTo(Description description) {
description.appendText("even numbers");
}

@Factory
public static <T> Matcher<Collection<Integer>> evenNumbers() {
return new AreEvenNumbers();
}
}

And below are two tests which uses the AreEvenNumbers custom matcher:

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static br.com.rafael.hamcrest.AreEvenNumbers.evenNumbers;

@Test
public void shouldHaveOnlyEvenNumbers()
{
List<Integer> numbers = Arrays.asList( 2, 4, 6, 8, 10 );
assertThat( numbers, is( evenNumbers() ) );
}

@Test
public void shouldNotHaveOddNumbers()
{
List<Integer> numbers = Arrays.asList( 1, 2, 4, 6, 8, 10 );
assertThat( numbers, not( evenNumbers() ) );
}

 These two tests use the static factory method evenNumbers to instantiate the matcher on the test code. Not the use of the not matcher on the shouldNotHaveOddNumbers test to assert that no odd numbers are present on the given list. All tests use the static import feature, which turns the test not clean and not cluttered with the class qualification.

I haven’t experienced the other common matchers on unit testing code, like the Beans, Collections and Number ones. I think they turn the tests more readable, clean and easy to change. And you? Have you ever used Hamcrest matcher? If you have other examples of using it, post them here!

 From http://rafaelnaufal.com/blog/2010/03/15/using-hamcrest-and-junit/

 

unit test Hamcrest JUnit

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 10 Programming Habits a Web Developer Should Embrace
  • How to Submit a Post to DZone
  • What Are Microservices?
  • Low Code and No Code: The Security Challenge

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • 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:

DZone.com is powered by 

AnswerHub logo