{"id":44057,"date":"2017-03-09T11:00:52","date_gmt":"2017-03-09T09:00:52","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=44057"},"modified":"2019-03-21T11:05:48","modified_gmt":"2019-03-21T09:05:48","slug":"junit-hamcrest-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/","title":{"rendered":"JUnit Hamcrest Example"},"content":{"rendered":"<p>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.<\/p>\n<p>If you want to see more example of JUnit, please visit <a href=\"https:\/\/examples.javacodegeeks.com\/author\/vinod-kashyap\/\" target=\"_blank\" rel=\"noopener noreferrer\">my series page<\/a>.<\/p>\n<p>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.<\/p>\n<h2>1. Introduction<\/h2>\n<p><a href=\"http:\/\/hamcrest.org\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Hamcrest<\/a> is a open source framework matcher library used in various language to match expression for your test cases. You can visit <a href=\"https:\/\/github.com\/hamcrest\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">github<\/a> page if you want to explore the code of it.<br \/>\nHamcrest has a very rich library of methods to fulfill our needs. It is used with different testing frameworks like JUnit and jMock.<\/p>\n<p>&nbsp;<br \/>\nHamcrest is typically viewed as a third generation matcher framework.<\/p>\n<ul>\n<li><strong>First Generation<\/strong>: It typically uses <code>assert(some statement)<\/code>. In this case tests are not easily readable.<\/li>\n<li><strong>Second Generation<\/strong>: It uses the special methods such as <code>assertEquals()<\/code> for test. But this approach creates a lots of assert methods.<\/li>\n<li><strong>Third Generation<\/strong>: It uses <code>assertThat()<\/code> 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.<\/li>\n<\/ul>\n<p>In our example we will use <code>assertThat()<\/code> for all our tests.<\/p>\n<h2>2. Technologies Used<\/h2>\n<ul>\n<li>Java<\/li>\n<li>JUnit 4.12 &#8211; Testing framework<\/li>\n<li>Hamcrest 1.3 &#8211; Used for matchers<\/li>\n<li>Eclipse &#8211; IDE for code<\/li>\n<li>Maven &#8211; dependency management tool<\/li>\n<\/ul>\n<h2>3. Project SetUp<\/h2>\n<div class=\"tip\"><strong>Tip<\/strong><br \/>\nYou may skip project creation and jump directly to the <a href=\"#code\"><strong>beginning of the example<\/strong><\/a> below.<\/div>\n<p>Open Eclipse. Select <code>File -&gt; New -&gt; Maven Project<\/code>. Fill in the following details.<\/p>\n<p><figure id=\"attachment_44065\" aria-describedby=\"caption-attachment-44065\" style=\"width: 725px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/03\/junit-hamcrest-setup-1.jpg\"><img decoding=\"async\" class=\"size-full wp-image-44065\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/03\/junit-hamcrest-setup-1.jpg\" alt=\"JUnit Hamcrest Example Setup 1\" width=\"725\" height=\"503\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/03\/junit-hamcrest-setup-1.jpg 725w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/03\/junit-hamcrest-setup-1-300x208.jpg 300w\" sizes=\"(max-width: 725px) 100vw, 725px\" \/><\/a><figcaption id=\"caption-attachment-44065\" class=\"wp-caption-text\">Figure 1: JUnit Hamcrest Example Setup 1<\/figcaption><\/figure><\/p>\n<p>After clicking Next button, you will be taken to following screen. Simply fill out all the necessary details and click on Finish button.<\/p>\n<p><figure id=\"attachment_44066\" aria-describedby=\"caption-attachment-44066\" style=\"width: 718px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/03\/junit-hamcrest-setup-2.jpg\"><img decoding=\"async\" class=\"size-full wp-image-44066\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/03\/junit-hamcrest-setup-2.jpg\" alt=\"JUnit Hamcrest Example Setup 2\" width=\"718\" height=\"661\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/03\/junit-hamcrest-setup-2.jpg 718w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/03\/junit-hamcrest-setup-2-300x276.jpg 300w\" sizes=\"(max-width: 718px) 100vw, 718px\" \/><\/a><figcaption id=\"caption-attachment-44066\" class=\"wp-caption-text\">Figure 2: JUnit Hamcrest Example Setup 2<\/figcaption><\/figure><\/p>\n<p><span id=\"code\"><\/span><br \/>\nThis will create an empty maven project.<\/p>\n<h2>4. JUnit Hamcrest Example<\/h2>\n<p>Start by writing following lines to the <code>pom.xml<\/code><br \/>\n<span style=\"text-decoration: underline;\"><em>pom.xml<\/em><\/span><\/p>\n<pre class=\"brush:xml\">&nbsp;&nbsp;&nbsp; &lt;dependencies&gt;\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;!-- https:\/\/mvnrepository.com\/artifact\/junit\/junit --&gt;\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;dependency&gt;\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;groupId&gt;junit&lt;\/groupId&gt;\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;artifactId&gt;junit&lt;\/artifactId&gt;\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;version&gt;4.12&lt;\/version&gt;\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;\/dependency&gt;\n\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;!-- https:\/\/mvnrepository.com\/artifact\/org.hamcrest\/hamcrest-library --&gt;\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;dependency&gt;\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;groupId&gt;org.hamcrest&lt;\/groupId&gt;\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;artifactId&gt;hamcrest-library&lt;\/artifactId&gt;\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;version&gt;1.3&lt;\/version&gt;\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&lt;\/dependency&gt;\n&nbsp;&nbsp; &nbsp;&lt;\/dependencies&gt;<\/pre>\n<p>If you simply write JUnit in <code>pom.xml<\/code>, it will fetch <code>hamcrest-core<\/code> jar with it. But we have also included <code>hamcrest-library<\/code> jar as we need to use different matchers that are not provided by default by JUnit.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>We will discuss details about each and every part of the example. First of all, let&#8217;s create a model class. This is a simple class that will help us to run our test cases.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Employee.java<\/em><\/span><\/p>\n<pre class=\"brush:java\">package junithamcrest;\n\nimport java.util.List;\n\npublic class Employee {\n\n\tprivate Long empId;\n\tprivate String empName;\n\tprivate String gender;\n\tprivate List awards;\n\n\tpublic Employee(Long empId, String empName, String gender, List awards) {\n\t\tsuper();\n\t\tthis.empId = empId;\n\t\tthis.empName = empName;\n\t\tthis.gender = gender;\n\t\tthis.awards = awards;\n\t}\n\n\tpublic Long getEmpId() {\n\t\treturn empId;\n\t}\n\n\tpublic void setEmpId(Long empId) {\n\t\tthis.empId = empId;\n\t}\n\n\tpublic String getEmpName() {\n\t\treturn empName;\n\t}\n\n\tpublic void setEmpName(String empName) {\n\t\tthis.empName = empName;\n\t}\n\n\tpublic String getGender() {\n\t\treturn gender;\n\t}\n\n\tpublic void setGender(String gender) {\n\t\tthis.gender = gender;\n\t}\n\n\tpublic List getAwards() {\n\t\treturn awards;\n\t}\n\n\tpublic void setAwards(List awards) {\n\t\tthis.awards = awards;\n\t}\n}\n<\/pre>\n<p>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.<\/p>\n<p><span style=\"text-decoration: underline;\"><em>JUnitHamcrestTestClass.java<\/em><\/span><\/p>\n<pre class=\"brush:java;wrap-lines:false\">package junithamcrest;\n\nimport static org.hamcrest.CoreMatchers.anyOf;\nimport static org.hamcrest.CoreMatchers.equalTo;\nimport static org.hamcrest.CoreMatchers.everyItem;\nimport static org.hamcrest.Matchers.allOf;\nimport static org.hamcrest.Matchers.containsString;\nimport static org.hamcrest.Matchers.endsWith;\nimport static org.hamcrest.Matchers.equalToIgnoringCase;\nimport static org.hamcrest.Matchers.hasProperty;\nimport static org.hamcrest.Matchers.hasSize;\nimport static org.hamcrest.Matchers.instanceOf;\nimport static org.hamcrest.Matchers.is;\nimport static org.hamcrest.Matchers.isA;\nimport static org.hamcrest.Matchers.isIn;\nimport static org.hamcrest.Matchers.notNullValue;\nimport static org.hamcrest.Matchers.startsWith;\nimport static org.hamcrest.Matchers.stringContainsInOrder;\nimport static org.hamcrest.Matchers.emptyCollectionOf;\nimport static org.junit.Assert.assertThat;\n\nimport java.util.ArrayList;\nimport java.util.Arrays;\nimport java.util.List;\n\nimport org.junit.BeforeClass;\nimport org.junit.Test;\n\npublic class JUnitHamcrestTestClass {\n\n\t\/\/ Creating instances\n\tprivate static Employee empA;\n\tprivate static Employee empB;\n\tprivate static List strList = Arrays.asList(\"Apple\", \"Apricot\", \"August\");\n\n\t@BeforeClass\n\tpublic static void init() {\n\n\t\t\/\/ creating objects \n\t\tempA = new Employee(1001L, \"Vinod Kumar Kashyap\", \"Male\", Arrays.asList(\"Best Team\", \"Star Employee\"));\n\t\tempB = new Employee(1002L, \"Asmi Kashyap\", \"Female\", Arrays.asList(\"Star Employee\"));\n\t}\n\n\t\/**\n\t * This method will test functionality of <code>is<\/code> matcher.\n\t *\/\n\t@Test\n\tpublic void isTest() {\n\t\t\/\/ checks that empA is an object of Employee class\n\t\tassertThat(empA, isA(Employee.class));\n\n\t\t\/\/ below are 3 versions of \"is\" method. All are same\n\t\tassertThat(2, equalTo(2));\n\t\tassertThat(2, is(equalTo(2)));\n\t\tassertThat(2, is(2));\n\t}\n\n\t\/**\n\t * This method will test functionality of <code>beans<\/code> matcher.\n\t *\/\n\t@Test\n\tpublic void beansTest() {\n\t\t\/\/ checks that object contains the property\n\t\tassertThat(empA, hasProperty(\"empName\"));\n\n\t\t\/\/ checks that the object contains the property with a value\n\t\tassertThat(empA, hasProperty(\"empName\", equalTo(\"Vinod Kumar Kashyap\")));\n\t}\n\n\t\/**\n\t * This method will test functionality of <code>collections<\/code> matcher.\n\t *\/\n\t@Test\n\tpublic void collectionsTest() {\n\t\t\/\/ checks that object is of checked size\n\t\tassertThat(empA.getAwards(), hasSize(2));\n\n\t\t\/\/ checks a collection for the element present\n\t\tassertThat(\"Best Team\", isIn(empA.getAwards()));\n\n\t\t\/\/ checks for empty collection\n\t\tassertThat(new ArrayList(), emptyCollectionOf(String.class));\n\t}\n\n\t\/**\n\t * This method will test functionality of <code>String<\/code> matcher.\n\t *\/\n\t@Test\n\tpublic void stringTest() {\n\t\tassertThat(empA.getEmpName(), containsString(\"Kumar\"));\n\t\tassertThat(empA.getEmpName(), endsWith(\"Kashyap\"));\n\t\tassertThat(empB.getEmpName(), startsWith(\"Asmi\"));\n\n\t\t\/\/ checks by ignoring case\n\t\tassertThat(empA.getEmpName(), equalToIgnoringCase(\"vinod KUMAR Kashyap\"));\n\n\t\t\/\/ checks that the elements are occurring in the same order\n\t\tassertThat(empA.getEmpName(), stringContainsInOrder(Arrays.asList(\"Vinod\", \"Kashyap\")));\n\t}\n\n\t\/**\n\t * Other common matchers\n\t *\/\n\t@Test\n\tpublic void otherCommonTest() {\n\t\t\/\/ all of the conditions should be met to pass the case\n\t\tassertThat(empB.getGender(), allOf(startsWith(\"F\"), containsString(\"ale\")));\n\n\t\t\/\/ any of the conditions should be met to pass the case\n\t\tassertThat(empB.getEmpName(), anyOf(startsWith(\"Dhwani\"), endsWith(\"yap\")));\n\n\t\t\/\/ checks that value is not null\n\t\tassertThat(empA, is(notNullValue()));\n\n\t\t\/\/ checks that object id instance of a Class\n\t\tassertThat(empA.getEmpId(), instanceOf(Long.class));\n\n\t\t\/\/ checks every item in  list\n\t\tassertThat(strList, everyItem(startsWith(\"A\")));\n\t}\n}\n\n<\/pre>\n<p>Now we will start with explanation part by part of the example. Most of the matchers are self explanatory.[ulp id=&#8217;ODQaBEw1BIbHApZq&#8217;]<\/p>\n<h3>4.1 Is Matcher<\/h3>\n<p>This is one of the most common matcher used.<\/p>\n<pre class=\"brush:java;highlight:[7,8,9]\">\t@Test\n\tpublic void isTest() {\n\t\t\/\/ checks that empA is an object of Employee class\n\t\tassertThat(empA, isA(Employee.class));\n\n\t\t\/\/ below are 3 versions of \"is\" method. All are same\n\t\tassertThat(2, equalTo(2));\n\t\tassertThat(2, is(equalTo(2)));\n\t\tassertThat(2, is(2));\n\t}\n<\/pre>\n<p><strong>Line no 7,8,9<\/strong> works exactly same. The last form is allowed since <code>is(T value)<\/code> is overloaded to return <code>is(equalTo(value))<\/code>.<\/p>\n<h3>4.2 Beans Matchers<\/h3>\n<p>These matchers are used to check out beans.<\/p>\n<pre class=\"brush:java\">\t@Test\n\tpublic void beansTest() {\n\t\t\/\/ checks that object contains the property\n\t\tassertThat(empA, hasProperty(\"empName\"));\n\n\t\t\/\/ checks that the object contains the property with a value\n\t\tassertThat(empA, hasProperty(\"empName\", equalTo(\"Vinod Kumar Kashyap\")));\n\t}\n<\/pre>\n<p>If you see here, we are testing that our class has a property associated with it or not.<\/p>\n<h3>4.3 Collections Matchers<\/h3>\n<p>These matchers works with the collections. JUnit and Hamcrest provides various ways to test collections.<\/p>\n<pre class=\"brush:java\">\t@Test\n\tpublic void collectionsTest() {\n\t\t\/\/ checks that object is of checked size\n\t\tassertThat(empA.getAwards(), hasSize(2));\n\n\t\t\/\/ checks a collection for the element present\n\t\tassertThat(\"Best Team\", isIn(empA.getAwards()));\n\n\t\t\/\/ checks for empty collection\n\t\tassertThat(new ArrayList(), emptyCollectionOf(String.class));\n\t}\n<\/pre>\n<h3>4.4 String Matchers<\/h3>\n<p>These matchers helps to work with Strings.<\/p>\n<pre class=\"brush:java;wrap-lines:false\">\t@Test\n\tpublic void stringTest() {\n\t\tassertThat(empA.getEmpName(), containsString(\"Kumar\"));\n\t\tassertThat(empA.getEmpName(), endsWith(\"Kashyap\"));\n\t\tassertThat(empB.getEmpName(), startsWith(\"Asmi\"));\n\n\t\t\/\/ checks by ignoring case\n\t\tassertThat(empA.getEmpName(), equalToIgnoringCase(\"vinod KUMAR Kashyap\"));\n\n\t\t\/\/ checks that the elements are occurring in the same order\n\t\tassertThat(empA.getEmpName(), stringContainsInOrder(Arrays.asList(\"Vinod\", \"Kashyap\")));\n\t}\n<\/pre>\n<h3>4.5 Other Common Matchers<\/h3>\n<p>There are many different matchers used. Here we are using some of the common matchers. It is also possible to chain matchers, via the <code>anyOf()<\/code> of<code> allOf()<\/code> method. See below for details.<\/p>\n<pre class=\"brush:java;highlight:[4,7,16]\">\t@Test\n\tpublic void otherCommonTest() {\n\t\t\/\/ all of the conditions should be met to pass the case\n\t\tassertThat(empB.getGender(), allOf(startsWith(\"F\"), containsString(\"ale\")));\n\n\t\t\/\/ any of the conditions should be met to pass the case\n\t\tassertThat(empB.getEmpName(), anyOf(startsWith(\"Dhwani\"), endsWith(\"yap\")));\n\n\t\t\/\/ checks that value is not null\n\t\tassertThat(empA, is(notNullValue()));\n\n\t\t\/\/ checks that object id instance of a Class\n\t\tassertThat(empA.getEmpId(), instanceOf(Long.class));\n\n\t\t\/\/ checks every item in  list\n\t\tassertThat(strList, everyItem(startsWith(\"A\")));\n\t}\n<\/pre>\n<p><strong>Line no 4<\/strong> uses <code>allOf()<\/code> matcher. It defines that all of the condition inside should match to pass the test.<br \/>\n<strong>Line no 7<\/strong> uses <code>anyOf()<\/code> 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.<br \/>\n<strong>Line no 16<\/strong> scans every item in a list and matches the condition. If condition is matched it will pass the test.<\/p>\n<h2>5. Output<\/h2>\n<p>For running the example simply right click on class and <code>Run As -&gt; Junit Test<\/code>. You will see the following output in JUnit console.<\/p>\n<p><figure id=\"attachment_44067\" aria-describedby=\"caption-attachment-44067\" style=\"width: 691px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/03\/junit-hamcrest-output.jpg\"><img decoding=\"async\" class=\"size-full wp-image-44067\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/03\/junit-hamcrest-output.jpg\" alt=\"JUnit Hamcrest Example Output\" width=\"691\" height=\"264\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/03\/junit-hamcrest-output.jpg 691w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/03\/junit-hamcrest-output-300x115.jpg 300w\" sizes=\"(max-width: 691px) 100vw, 691px\" \/><\/a><figcaption id=\"caption-attachment-44067\" class=\"wp-caption-text\">Figure 3: JUnit Hamcrest Example Output<\/figcaption><\/figure><\/p>\n<h2>5. Conclusion<\/h2>\n<p>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.<\/p>\n<h2>6. Download the Eclipse project<\/h2>\n<p>This is JUnit Hamcrest Example.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>\nYou can download the full source code of this example here: <a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2017\/03\/junithamcrest.zip\"><strong>JUnitHamcrest.zip<\/strong><\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip;<\/p>\n","protected":false},"author":112,"featured_media":6678,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[994],"class_list":["post-44057","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-junit","tag-hamcrest"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JUnit Hamcrest Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"In this example we shall show users the usage of JUnit Hamcrest. Through JUnit Hamcrest Example we will show what is hamcrest, where it is used, why used\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JUnit Hamcrest Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"In this example we shall show users the usage of JUnit Hamcrest. Through JUnit Hamcrest Example we will show what is hamcrest, where it is used, why used\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2017-03-09T09:00:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-03-21T09:05:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Vinod Kumar Kashyap\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vinodkashyap\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vinod Kumar Kashyap\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/\"},\"author\":{\"name\":\"Vinod Kumar Kashyap\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/1d6281e8235e49b5b8614621c2445d2c\"},\"headline\":\"JUnit Hamcrest Example\",\"datePublished\":\"2017-03-09T09:00:52+00:00\",\"dateModified\":\"2019-03-21T09:05:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/\"},\"wordCount\":808,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg\",\"keywords\":[\"hamcrest\"],\"articleSection\":[\"junit\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/\",\"name\":\"JUnit Hamcrest Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg\",\"datePublished\":\"2017-03-09T09:00:52+00:00\",\"dateModified\":\"2019-03-21T09:05:48+00:00\",\"description\":\"In this example we shall show users the usage of JUnit Hamcrest. Through JUnit Hamcrest Example we will show what is hamcrest, where it is used, why used\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Development\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"junit\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/junit\/\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"JUnit Hamcrest Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/1d6281e8235e49b5b8614621c2445d2c\",\"name\":\"Vinod Kumar Kashyap\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a11634ec460c849e8631b740af1b75cb68c9c58d5c2339c21bcd95f55b9af4f5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/a11634ec460c849e8631b740af1b75cb68c9c58d5c2339c21bcd95f55b9af4f5?s=96&d=mm&r=g\",\"caption\":\"Vinod Kumar Kashyap\"},\"description\":\"Vinod is Sun Certified and love to work in Java and related technologies. Having more than 13 years of experience, he had developed software's including technologies like Java, Hibernate, Struts, Spring, HTML 5, jQuery, CSS, Web Services, MongoDB, AngularJS, AWS. He is also a JUG Leader of Chandigarh Java User Group.\",\"sameAs\":[\"http:\/\/www.vinodkashyap.com\/\",\"https:\/\/www.instagram.com\/vinodkashyap\/\",\"https:\/\/in.linkedin.com\/in\/vinodkashyap\",\"https:\/\/in.pinterest.com\/vinodkashyap\/\",\"https:\/\/x.com\/vinodkashyap\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/vinod-kashyap\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JUnit Hamcrest Example - Java Code Geeks","description":"In this example we shall show users the usage of JUnit Hamcrest. Through JUnit Hamcrest Example we will show what is hamcrest, where it is used, why used","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/","og_locale":"en_US","og_type":"article","og_title":"JUnit Hamcrest Example - Java Code Geeks","og_description":"In this example we shall show users the usage of JUnit Hamcrest. Through JUnit Hamcrest Example we will show what is hamcrest, where it is used, why used","og_url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2017-03-09T09:00:52+00:00","article_modified_time":"2019-03-21T09:05:48+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg","type":"image\/jpeg"}],"author":"Vinod Kumar Kashyap","twitter_card":"summary_large_image","twitter_creator":"@vinodkashyap","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Vinod Kumar Kashyap","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/"},"author":{"name":"Vinod Kumar Kashyap","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/1d6281e8235e49b5b8614621c2445d2c"},"headline":"JUnit Hamcrest Example","datePublished":"2017-03-09T09:00:52+00:00","dateModified":"2019-03-21T09:05:48+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/"},"wordCount":808,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg","keywords":["hamcrest"],"articleSection":["junit"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/","url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/","name":"JUnit Hamcrest Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg","datePublished":"2017-03-09T09:00:52+00:00","dateModified":"2019-03-21T09:05:48+00:00","description":"In this example we shall show users the usage of JUnit Hamcrest. Through JUnit Hamcrest Example we will show what is hamcrest, where it is used, why used","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2013\/11\/junit-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/junit\/junit-hamcrest-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"},{"@type":"ListItem","position":4,"name":"junit","item":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/junit\/"},{"@type":"ListItem","position":5,"name":"JUnit Hamcrest Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/1d6281e8235e49b5b8614621c2445d2c","name":"Vinod Kumar Kashyap","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/a11634ec460c849e8631b740af1b75cb68c9c58d5c2339c21bcd95f55b9af4f5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a11634ec460c849e8631b740af1b75cb68c9c58d5c2339c21bcd95f55b9af4f5?s=96&d=mm&r=g","caption":"Vinod Kumar Kashyap"},"description":"Vinod is Sun Certified and love to work in Java and related technologies. Having more than 13 years of experience, he had developed software's including technologies like Java, Hibernate, Struts, Spring, HTML 5, jQuery, CSS, Web Services, MongoDB, AngularJS, AWS. He is also a JUG Leader of Chandigarh Java User Group.","sameAs":["http:\/\/www.vinodkashyap.com\/","https:\/\/www.instagram.com\/vinodkashyap\/","https:\/\/in.linkedin.com\/in\/vinodkashyap","https:\/\/in.pinterest.com\/vinodkashyap\/","https:\/\/x.com\/vinodkashyap"],"url":"https:\/\/examples.javacodegeeks.com\/author\/vinod-kashyap\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/44057","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/112"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=44057"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/44057\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/6678"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=44057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=44057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=44057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}