{"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\"> 3<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span><p class=\"lead\"><!-- pn-tldr --><\/p>\n<h2>Key takeaways<\/h2>\n<ul>\n<li>Categories let one suite be sliced by speed or area, so CI can run the fast set per commit.<\/li>\n<li>Group by what you want to run together, not by package layout.<\/li>\n<li>Without a grouping strategy the whole suite runs every time, and then gets skipped.<\/li>\n<\/ul>\n<p><!-- \/pn-tldr --><\/p>\n<h3>Why structure your integration tests ?<\/h3>\n<p>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<p><!-- pn-faq --><\/p>\n<h2>Frequently Asked Questions<\/h2>\n<h3>What problem do JUnit categories solve?<\/h3>\n<p>They let one suite be sliced several ways: by feature, by API, or by how long the tests take. CI can then run the fast set on every commit and keep the slow set for a nightly job.<\/p>\n<h3>Should I group by package instead?<\/h3>\n<p>Package layout follows the code, not what you want to run together. Categories are an independent axis, so a test can belong to a feature group and a speed group at the same time.<\/p>\n<h3>What happens without a grouping strategy?<\/h3>\n<p>The whole suite runs every time. It gets slower, then people stop waiting for it, and eventually it is skipped on the commits that most needed it.<\/p>\n<p><!-- \/pn-faq --><!-- pn-related-reading --><\/p>\n<h2>Related reading<\/h2>\n<ul>\n<li><a href=\"https:\/\/loadfocus.com\/blog\/tech\/2017\/03\/how-to-use-the-dataprovider-in-testng-with-a-java-example\">How to Use the DataProvider in TestNG with a JAVA Example<\/a><\/li>\n<li><a href=\"https:\/\/loadfocus.com\/blog\/tech\/2018\/02\/mocking-apis-using-wiremock-for-testing-negative-cases-when-it-is-not-possible-to-change-the-api-behaviour\">Mocking APIs With WireMock to Test Negative Cases<\/a><\/li>\n<li><a href=\"https:\/\/loadfocus.com\/blog\/tech\/2018\/07\/best-practices-for-implementing-e2e-end-to-end-ui-tests\">Best Practices for Implementing E2E UI Tests<\/a><\/li>\n<\/ul>\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\"> 3<\/span> <span class=\"rt-label rt-postfix\">minutes read<\/span><\/span>Key takeaways Categories let one suite be sliced by speed or area, so CI can run the fast set per commit. Group by what you want to run together, not by package layout. Without a grouping strategy the whole suite runs every time, and then gets skipped. Why structure your integration tests ? When 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":508,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[37,36],"class_list":["post-205","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","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:featuredmedia":[{"embeddable":true,"href":"https:\/\/loadfocus.com\/blog\/tech\/wp-json\/wp\/v2\/media\/508"}],"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}]}}