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. Languages
  4. hasProperty, the Hidden Gem of Hamcrest (and assertThat)

hasProperty, the Hidden Gem of Hamcrest (and assertThat)

Jakub Holý user avatar by
Jakub Holý
·
Oct. 18, 11 · Interview
Like (0)
Save
Tweet
Share
6.13K Views

Join the DZone community and get the full member experience.

Join For Free

If you got used to JUnit 4′s assertThat with various matchers (of course you will need junit-dep.jar and hamcrest.jar to get the full set instead of the small subset integrated in junit.jar), make sure you don’t overlook the matcher hasProperty. It is very useful if you have non-trivial objects and cannot use some more flexible language like Groovy for your unit tests.

The advantage of hasProperty is that it allows you to check a particular property (or more properties with allOf) of an object while ignoring the others – pretty useful if the object has 20 properties and checking just one is enough for you. (Admittedly, an object with 20 properties is an abomination but hey, that’s the real legacy word!)

Example – check that collection contains two Images with some file names:

assertThat("Expected images", (Iterable<Object>) hotel.getImages()
  , containsInAnyOrder(hasProperty("filename", is("radisson1.jpg"))
     , hasProperty("filename", is("radisson2.jpg"))));

The failure message in this case isn’t as clear as I might wish but still this is the best solution I can think of.

From http://theholyjava.wordpress.com/2011/10/15/hasproperty-the-hidden-gem-of-hamcrest-and-assertthat/

Hamcrest GEM (desktop environment) Property (programming) Object (computer science) Groovy (programming language) Subset Testing JUnit Advantage (cryptography)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Best Navicat Alternative for Windows
  • File Uploads for the Web (2): Upload Files With JavaScript
  • Building Microservice in Golang
  • Mocha JavaScript Tutorial With Examples for Selenium Testing

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: