{"id":205,"date":"2018-02-23T11:51:59","date_gmt":"2018-02-23T11:51:59","guid":{"rendered":"http:\/\/loadfocus.com\/blog\/tech\/?p=205"},"modified":"2022-03-04T11:46:17","modified_gmt":"2022-03-04T11:46:17","slug":"structuring-integration-tests-using-junit-categories-by-features-api-or-type-of-tests","status":"publish","type":"post","link":"https:\/\/loadfocus.com\/blog\/tech\/2018\/02\/structuring-integration-tests-using-junit-categories-by-features-api-or-type-of-tests","title":{"rendered":"Structuring integration tests using JUnit categories by features, API or type of tests"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 2<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span><h3>Why structure your integration tests ?<\/h3>\n<p class=\"lead\">When the number of integration tests grow is a good practice to separate the tests based on the feature\/api that they are testing.<\/p>\n<p>The advantages of doing this are:<\/p>\n<ul>\n<li>easier to maitain as the tests for a specific feature\/API are grouped in a specific category<\/li>\n<li>easier to run the tests only for that specific category when only that feature changes to get faster feedback<\/li>\n<li>easier to group the tests when running them in Jenkins\/Travis CI or any other CI so that multiple jobs can be run in parallel and also when a job fails imediatelly the team will know which feature\/API has failed<\/li>\n<\/ul>\n<h3>How is this done using Java, JUnit and Maven ?<\/h3>\n<ol>\n<li>Create a basic Maven project<\/li>\n<li>Create a new package in the src\/main\/java with the name &#8220;tutorial.junit.categories&#8221; in which the different categories will be added.<br \/>\nThe structure of the project should look like below:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-224 size-full\" src=\"https:\/\/loadfocus.com\/blog\/wp-content\/uploads\/sites\/2\/2018\/02\/Screen-Shot-2018-02-23-at-10.14.46-1.png\" alt=\"\" width=\"2046\" height=\"736\" \/><\/li>\n<li>Create a few JUnit test cases in a few test classes. In our case we are going to create:\n<ul>\n<li>\u00a0LoginApiTestIT.java<\/li>\n<li>\u00a0SearchApiTestIT.java<\/li>\n<\/ul>\n<\/li>\n<li>Create a few categories in which we want to split the tests. In our case we are going to split them as follows:\n<ul>\n<li>LoginApiCategory &#8211; tests run against the login api<\/li>\n<li>SearchApiCategory &#8211; tests run against the search api<\/li>\n<li>SmokeTestCategory &#8211; which includes tests from either Login or Search API tests that we want to run as smoke tests<\/li>\n<\/ul>\n<\/li>\n<li>A category is just an empty interface as in the example below:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-225 size-full\" src=\"https:\/\/loadfocus.com\/blog\/wp-content\/uploads\/sites\/2\/2018\/02\/Screen-Shot-2018-02-23-at-10.37.57-1.png\" alt=\"\" width=\"2046\" height=\"466\" \/><\/li>\n<li>Setting the category for each of the tests. For setting the category we just need to add the @Category annotation\u00a0either at the class or method level as\u00a0 below.<\/li>\n<li>Example below:\n<pre class=\"lang-java\"><code class=\"lang-java\">\npackage tutorial.junit;\n\nimport org.junit.Test;\nimport org.junit.experimental.categories.Category;\n\nimport tutorial.junit.categories.LoginApiCategory;\nimport tutorial.junit.categories.SmokeTestCategory;\n\n @Category(LoginApiCategory.class)\n public class LoginApiTestIT {\n\n @Category(SmokeTestCategory.class)\n @Test\n public void shouldReturn200ForValidCredentials(){\n    System.out.println(\"Running Login API tests - Positive case\");\n }\n\n@Test\n public void shouldReturn404ForInvalidCredentials(){\n    System.out.println(\"Running Login API tests - Negative case\");\n  }\n}\n\n<\/code><\/pre>\n<\/li>\n<li>Running the tests based on the category is pretty simple using the Maven commands below. Open the Maven pom.xml file location in your terminal and run the following commands:\n<ul>\n<li>running the login tests\n<pre class=\"lang-java\"><code class=\"lang-java\">\nmvn clean install -P integration-test -Dgroups=\"tutorial.junit.categories.LoginApiCategory\"<\/code><\/pre>\n<\/li>\n<li>running the search tests\n<pre class=\"lang-java\"><code class=\"lang-java\">\nmvn clean install -P integration-test -Dgroups=\"tutorial.junit.categories.SearchApiCategory\"<\/code><\/pre>\n<\/li>\n<li>running the smoke tests which include tests from login and search\n<pre class=\"lang-java\"><code class=\"lang-java\">mvn clean install -P integration-test -Dgroups=\"tutorial.junit.categories.SmokeTestCategory\"<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\"><\/span> <span class=\"rt-time\"> 2<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span>Why structure your integration tests ? When the number of integration tests grow is a good practice to separate the tests based on the feature\/api that they are testing. The advantages of doing this are: easier to maitain as the tests for a specific feature\/API are grouped in a specific category easier to run the&#8230;  <a href=\"https:\/\/loadfocus.com\/blog\/tech\/2018\/02\/structuring-integration-tests-using-junit-categories-by-features-api-or-type-of-tests\" class=\"more-link\" title=\"Read Structuring integration tests using JUnit categories by features, API or type of tests\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,35],"tags":[37,36],"class_list":["post-205","post","type-post","status-publish","format-standard","hentry","category-java","category-junit","tag-integration-tests","tag-junit-categories"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/comments?post=205"}],"version-history":[{"count":25,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/205\/revisions"}],"predecessor-version":[{"id":398,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/posts\/205\/revisions\/398"}],"wp:attachment":[{"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/media?parent=205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/categories?post=205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/tags?post=205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}