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 > Some useful new Hamcrest matchers for collections

Some useful new Hamcrest matchers for collections

John Ferguson Smart user avatar by
John Ferguson Smart
·
Dec. 16, 11 · Java Zone · Interview
Like (0)
Save
Tweet
9.98K Views

Join the DZone community and get the full member experience.

Join For Free

Hamcrest is a neat little library that lets you write more fluent and readable tests. For example, rather than writing:

    assertEquals("red", color);

you would write:

    assertThat(color,is("red"));
This makes for tests that express their intent much more clearly, which in turn makes the tests easier to understand and to maintain, and more likely to be correct. This is generally considered to be a Good Thing.

A version of Hamcrest is actually bundled with JUnit. However it is somewhat dated, and the more recent versions of Hamcrest come with a lot more features, particularly with regards to working with collections. You can use the latest version of Hamcrest by using the junit-dep dependency instead of junit, and configuring your dependencies as shown here:

 <dependency>
	    <groupId>junit</groupId>
	    <artifactId>junit-dep</artifactId>
	    <version>4.10</version>
	    <scope>test</scope>
	    <exclusions>
	        <exclusion>
	            <groupId>org.hamcrest</groupId>
	            <artifactId>hamcrest-core</artifactId>
	        </exclusion>
	    </exclusions>
	</dependency>
	<dependency>
	    <groupId>org.hamcrest</groupId>
	    <artifactId>hamcrest-library</artifactId>
	    <version>1.3.RC2</version>
	</dependency>

You can check the size of a collection directly:

    List colors = Arrays.asList("red","green","blue");
    assertThat(colors, hasSize(3));
You can also check the exact contents of a collection:
 List colors = Arrays.asList("red","green","blue");
    assertThat(colors, contains("red", "green", "blue"));

This last example checks both the contents and the order of the collection. If you are only interested in the contents, you can use containsInAnyOrder:

   List colors = Arrays.asList("red","green","blue");
    assertThat(colors, containsInAnyOrder("green", "red", "blue"));

 

You can also check that a condition is true for every item in a list:

    List ages = Arrays.asList(21, 25,30);
    assertThat(ages, everyItem(greaterThan(18)));

This version is a release candidate, so there might be a few rough edges before the final version. And since the artifact names have changed, you need to be careful to exclude any references to hamcrest-all used by other libraries (lambdaj, for example, uses Hamcrest).

 

From http://weblogs.java.net/blog/johnsmart/archive/2011/12/12/some-useful-new-hamcrest-matchers-collections

Hamcrest

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Implementing HIPAA Technical Safeguards in Your API Platform
  • How BDD Works Well With EDA
  • Legacy Modernization and Hybrid Cloud with Kafka in Healthcare
  • Kotlin vs Java: Which One Is the Best?

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