About Vinod Kumar Kashyap

Vinod is Sun Certified and love to work in Java and related technologies. Having more than 10 years of experience, he had developed software’s including technologies like Java, Hibernate, Struts, Spring, HTML 5, jQuery, CSS, Web Services, MongoDB, AngularJS. He is also a JUG Leader of Chandigarh Java User Group.

JUnit Hamcrest Example

In this example we shall show users the usage of Hamcrest. Through JUnit Hamcrest Example we will show users what is hamcrest, where it is used, why it is used, when it is used and how to use it on your applications. If you are a regular user of my JUnit series then you are already familiar with the JUnit.

If you want to see more example of JUnit, please visit my series page.

We will start by getting a little bit information about the hamcrest. It has a very good integration with JUnit and both provides a good framework for testing.

1. Introduction

Hamcrest is a open source framework matcher library used in various language to match expression for your test cases. You can visit github page if you want to explore the code of it.
Hamcrest has a very rich library of methods to fulfill our needs. It is used with different testing frameworks like JUnit and jMock.

Want to be a JUnit Master ?

Subscribe to our newsletter and download the JUnit Programming Cookbook right now!

In order to help you master unit testing with JUnit, we have compiled a kick-ass guide with all the major JUnit features and use cases! Besides studying them online you may download the eBook in PDF format!

Email address:

Leave this field empty if you're human:

Hamcrest is typically viewed as a third generation matcher framework.

First Generation: It typically uses assert(some statement). In this case tests are not easily readable.

Second Generation: It uses the special methods such as assertEquals() for test. But this approach creates a lots of assert methods.

Third Generation: It uses assertThat() method for test. It is more flexible and covers most of the scenarios. The benefit is that you still get fluent error messages when the assertion fails, but now you have greater extensibility.

If you simply write JUnit in pom.xml, it will fetch hamcrest-core jar with it. But we have also included hamcrest-library jar as we need to use different matchers that are not provided by default by JUnit.

We will discuss details about each and every part of the example. First of all, let’s create a model class. This is a simple class that will help us to run our test cases.

Now create a test class, so that we can test above class. This class will test all hamcrest matchers. We have tried to cover most common but users are advised to see other matchers if they want to dig more deeper.

4.5 Other Common Matchers

There are many different matchers used. Here we are using some of the common matchers. It is also possible to chain matchers, via the anyOf() of allOf() method. See below for details.

@Test
public void otherCommonTest() {
// all of the conditions should be met to pass the case
assertThat(empB.getGender(), allOf(startsWith("F"), containsString("ale")));
// any of the conditions should be met to pass the case
assertThat(empB.getEmpName(), anyOf(startsWith("Dhwani"), endsWith("yap")));
// checks that value is not null
assertThat(empA, is(notNullValue()));
// checks that object id instance of a Class
assertThat(empA.getEmpId(), instanceOf(Long.class));
// checks every item in list
assertThat(strList, everyItem(startsWith("A")));
}

Line no 4 uses allOf() matcher. It defines that all of the condition inside should match to pass the test.Line no 7 uses anyOf() matcher, which tells us that if any of the condition is matched, test case will pass. As you see that in our scenario first condition is not matched but second one does. This case passes with flying colors. Because any one of the conditions is true.Line no 16 scans every item in a list and matches the condition. If condition is matched it will pass the test.

5. Output

For running the example simply right click on class and Run As -> Junit Test. You will see the following output in JUnit console.

Figure 3: JUnit Hamcrest Example Output

5. Conclusion

JUnit Hamcrest Example focus mostly on the usage of the JUnit Hamcrest matchers. You have learned what is hamcrest, why we should use it, where it should be used, how to use it. This example shows the usage of a simple example which covers most if the matchers.

6. Download the Eclipse project

This is JUnit Hamcrest Example.

Download
You can download the full source code of this example here: JUnitHamcrest.zip

Leave a Reply

Newsletter

Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

Email address:

Receive Java & Developer job alerts in your Area

Leave this field empty if you're human:

Join Us

With 1,240,600 monthly unique visitors and over 500 authors we are placed among the top Java related sites around. Constantly being on the lookout for partners; we encourage you to join us. So If you have a blog with unique and interesting content then you should check out our JCG partners program. You can also be a guest writer for Java Code Geeks and hone your writing skills!

Disclaimer

All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners. Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries. Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.