{"id":12677,"date":"2013-08-10T18:47:00","date_gmt":"2013-08-10T17:47:00","guid":{"rendered":"https:\/\/touk.pl\/blog\/?guid=0111e52c5a78e32e2aeea278d9ed4fda"},"modified":"2022-08-03T09:27:02","modified_gmt":"2022-08-03T07:27:02","slug":"integration-tests-with-maven-and-junit","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2013\/08\/10\/integration-tests-with-maven-and-junit\/","title":{"rendered":"Integration tests with Maven and JUnit"},"content":{"rendered":"<p>There is no doubt that integration tests phase is crucial in modern applications development. We need to test behaviour of our subsystems and how they interact with other modules. Using JUnit and Maven it&#8217;s quite easy to create integration tests and run them in separate phase than unit test. It is very important, because integration tests tend to take much more time than unit ones because they work with database, network connections, other subsystems etc. Therefore, we want to run them more rarely. With JUnit in version &gt;= 4.8 there are two approaches for creating and running integration test:<br \/>\n* using naming conventions and specifying separate executions for maven-surefire plugin<br \/>\n* create marking interface and mark integration tests with @Category annotation and run test from failsafe-plugin (although it is possible to use surefire in both cases)<\/p>\n<h3 id=\"separate-executions-first-method-needs-naming-convention-like-naming-all-unit-tests-with-test-java-postfix-or-spec-groovy-and-integration-tests-with-integr\">Separate executions First method needs naming convention like naming all unit tests with &#8220;..Test.java&#8221; postfix (or &#8220;..Spec.groovy&#8221; ;) and integration tests with &#8220;..IntegrationTest.java&#8221;. Then we need to change maven surefire configuration:<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;plugin&gt;\r\n    &lt;groupId&gt;org.apache.maven.plugins&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;maven-surefire-plugin&lt;\/artifactId&gt;\r\n    &lt;version&gt;2.15&lt;\/version&gt;\r\n    &lt;configuration&gt;\r\n        &lt;skip&gt;true&lt;\/skip&gt;    \r\n    &lt;\/configuration&gt;\r\n&lt;\/plugin&gt;\r\n<\/pre>\n<p>What I did here is forcing maven to skip default test phase. Instead of that, I will configure two separate executions (just below the \u00a0section):<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;executions&gt;\r\n    &lt;execution&gt;\r\n        &lt;id&gt;unit-tests&lt;\/id&gt;\r\n        &lt;phase&gt;test&lt;\/phase&gt;\r\n        &lt;goals&gt;\r\n            &lt;goal&gt;test&lt;\/goal&gt;\r\n        &lt;\/goals&gt;\r\n        &lt;configuration&gt;\r\n            &lt;skip&gt;false&lt;\/skip&gt;\r\n            &lt;includes&gt;\r\n                &lt;include&gt;**\/*Test.class&lt;\/include&gt;\r\n                &lt;include&gt;**\/*Spec.class&lt;\/include&gt;\r\n            &lt;\/includes&gt;\r\n            &lt;excludes&gt;\r\n                &lt;exclude&gt;**\/*IntegrationTest.class&lt;\/exclude&gt;\r\n            &lt;\/excludes&gt;\r\n        &lt;\/configuration&gt;\r\n    &lt;\/execution&gt;\r\n    &lt;execution&gt;\r\n        &lt;id&gt;integration-tests&lt;\/id&gt;\r\n        &lt;phase&gt;integration-test&lt;\/phase&gt;\r\n        &lt;goals&gt;\r\n            &lt;goal&gt;test&lt;\/goal&gt;\r\n        &lt;\/goals&gt;\r\n        &lt;configuration&gt;\r\n            &lt;skip&gt;false&lt;\/skip&gt;\r\n            &lt;includes&gt;\r\n                &lt;include&gt;**\/*IntegrationTest.class&lt;\/include&gt;\r\n            &lt;\/includes&gt;\r\n        &lt;\/configuration&gt;\r\n    &lt;\/execution&gt;\r\n&lt;\/executions&gt;\r\n<\/pre>\n<p>In unit test execution I include all test that match naming convention for unit tests (both JUnit and spock ones) and exclude files matching integration test pattern and in integration test execution I did something opposite ;)<\/p>\n<h3 id=\"annotations\">Annotations<\/h3>\n<div>Another method requires defining of marking interface like this:<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">package info.rnowak.webtex.common.test;\r\n\r\npublic interface IntegrationTest {\r\n\r\n}\r\n<\/pre>\n<p>Then we can mark our integration test class with:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">@Category(IntegrationTest.class)\r\n<\/pre>\n<p>Next thing is changing of surefire plugin configuration to omit integration test:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;plugin&gt;\r\n    &lt;groupId&gt;org.apache.maven.plugins&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;maven-surefire-plugin&lt;\/artifactId&gt;\r\n    &lt;version&gt;2.15&lt;\/version&gt;\r\n    &lt;configuration&gt;\r\n        &lt;includes&gt;\r\n            &lt;include&gt;**\/*Test.class&lt;\/include&gt;\r\n            &lt;include&gt;**\/*Spec.class&lt;\/include&gt;\r\n        &lt;\/includes&gt;  \r\n        &lt;excludedGroups&gt;info.rnowak.webtex.common.test.IntegrationTest&lt;\/excludedGroups&gt; \r\n    &lt;\/configuration&gt;\r\n&lt;\/plugin&gt;\r\n<\/pre>\n<p>What has changed here is new tag with name of interface which marks our integration tests. Next, we need to add and configure maven-failsafe plugin in order to run test from out integration test group:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;plugin&gt;&lt;plugin&gt;\r\n    &lt;groupId&gt;org.apache.maven.plugins&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;maven-failsafe-plugin&lt;\/artifactId&gt;\r\n    &lt;version&gt;2.15&lt;\/version&gt;\r\n    &lt;executions&gt;\r\n        &lt;execution&gt;\r\n            &lt;goals&gt;\r\n                &lt;goal&gt;integration-test&lt;\/goal&gt;\r\n            &lt;\/goals&gt;\r\n            &lt;configuration&gt;\r\n                &lt;groups&gt;info.rnowak.webtex.common.test.IntegrationTest&lt;\/groups&gt;\r\n                &lt;includes&gt;\r\n                    &lt;include&gt;**\/*.class&lt;\/include&gt;\r\n                &lt;\/includes&gt;\r\n            &lt;\/configuration&gt;\r\n        &lt;\/execution&gt;\r\n    &lt;\/executions&gt;\r\n&lt;\/plugin&gt;\r\n<\/pre>\n<p>With this configuration failsafe will run only test marked with <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">@Category(IntegrationTest.class)<\/code>annotation, no matter what their names are.<\/p>\n<h3 id=\"what-is-better-well-in-my-opinion-its-just-a-matter-of-taste-and-style-annotating-each-integration-class-may-be-a-little-cumbersome-but-we-are-not-limited-to-naming-classes-within-specified\">What is better? Well, in my opinion it&#8217;s just a matter of taste and style. Annotating each integration class may be a little cumbersome but we are not limited to naming classes within specified convention. On the other hand, unit test and integration test usually are named with some convention, so annotations are not a big deal.<\/h3>\n","protected":false},"excerpt":{"rendered":"There is no doubt that integration tests phase is crucial in modern applications development. We need to test behaviour of our subsystems and how they interact with other modules.Using JUnit and Maven it&#8217;s quite easy to create integration tests and run&#8230;\n","protected":false},"author":48,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[68,90,30],"class_list":{"0":"post-12677","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-java","8":"tag-maven","9":"tag-testing"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/users\/48"}],"replies":[{"embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/comments?post=12677"}],"version-history":[{"count":5,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12677\/revisions"}],"predecessor-version":[{"id":14898,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12677\/revisions\/14898"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=12677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=12677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=12677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}