{"id":12673,"date":"2013-08-08T22:36:00","date_gmt":"2013-08-08T21:36:00","guid":{"rendered":"https:\/\/touk.pl\/blog\/?guid=e258117877c04cf766d376ac47686f1e"},"modified":"2022-08-01T11:43:21","modified_gmt":"2022-08-01T09:43:21","slug":"spock-java-and-maven","status":"publish","type":"post","link":"https:\/\/touk.pl\/blog\/2013\/08\/08\/spock-java-and-maven\/","title":{"rendered":"Spock, Java and Maven"},"content":{"rendered":"<div style=\"text-align: justify\">Few months ago I\u2019ve came across Groovy \u2013 powerful language for JVM platform which combines the power of Java with abilities typical for scripting languages (dynamic typing, metaprogramming).<\/div>\n<div style=\"text-align: justify\"><\/div>\n<div style=\"text-align: justify\">Together with Groovy I\u2019ve discovered spock framework (<a href=\"https:\/\/code.google.com\/p\/spock\/\">https:\/\/code.google.com\/p\/spock\/<\/a>) \u2013 specification framework for Groovy (of course you can test Java classes too!). But spock is not only test\/specification framework \u2013 it also contains powerful mocking tools.<\/div>\n<div style=\"text-align: justify\"><\/div>\n<div style=\"text-align: justify\">Even though spock is dedicated for Groovy there is no problem with using it for Java classes tests. In this post I\u2019m going to describe how to configure Maven project to build and run spock specifications together with traditional JUnit tests.<\/div>\n<div style=\"text-align: justify\"><\/div>\n<div style=\"text-align: justify\">Firstly, we need to prepare pom.xml and add necessary dependencies and plugins.<\/div>\n<div style=\"text-align: justify\"><\/div>\n<div style=\"text-align: justify\">Two obligatory libraries are:<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\"><dependency>\r\n    <groupid>org.spockframework<\/groupId>\r\n    <artifactid>spock-core<\/artifactId>\r\n    <version>0.7-groovy-2.0<\/version>\r\n    <scope>test<\/scope>\r\n<\/dependency>\r\n<dependency>\r\n    <groupid>org.codehaus.groovy<\/groupId>\r\n    <artifactid>groovy-all<\/artifactId>\r\n    <version>${groovy.version}<\/version>\r\n    <scope>test<\/scope>\r\n<\/dependency>\r\n<\/pre>\n<p>Where groovy.version is property defined in pom.xml for more convenient use and easy version change, just like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\"><properties>\r\n    <gmaven-plugin.version>1.4<\/gmaven-plugin.version>\r\n    <groovy.version>2.1.5<\/groovy.version>\r\n<\/properties>\r\n<\/pre>\n<p>\u00a0<\/p>\n<div style=\"text-align: justify\">I\u2019ve added property for gmaven-plugin version for the same reason ;)<\/div>\n<div style=\"text-align: justify\"><\/div>\n<div style=\"text-align: justify\">Besides these two dependencies, we can use few additional ones providing extra functionality:<\/div>\n<p>\u00a0<\/p>\n<ul>\n<li style=\"text-align: justify\"><b>cglib<\/b> \u2013 for class mocking<\/li>\n<li style=\"text-align: justify\"><b>objenesis<\/b> \u2013 enables mocking classes without default constructor<\/li>\n<\/ul>\n<p>\u00a0<\/p>\n<div style=\"text-align: justify\">To add them to the project put these lines in section of pom.xml:<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\"><dependency>\r\n    <groupid>cglib<\/groupId>\r\n    <artifactid>cglib-nodep<\/artifactId>\r\n    <version>3.0<\/version>\r\n    <scope>test<\/scope>\r\n<\/dependency>\r\n<dependency>\r\n    <groupid>org.objenesis<\/groupId>\r\n    <artifactid>objenesis<\/artifactId>\r\n    <version>1.3<\/version>\r\n    <scope>test<\/scope>\r\n<\/dependency>\r\n<\/pre>\n<div style=\"text-align: justify\">And that\u2019s all for dependencies section. Now we will focus on plugins necessary to compile Groovy classes. We need to add gmaven-plugin with gmaven-runtime-2.0 dependency in plugins section:<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\"><plugin>\r\n    <groupid>org.codehaus.gmaven<\/groupId>\r\n    <artifactid>gmaven-plugin<\/artifactId>\r\n    <version>${gmaven-plugin.version}<\/version>\r\n    <configuration>\r\n        <providerselection>2.0<\/providerSelection>\r\n    <\/configuration>\r\n    <executions>\r\n        <execution>\r\n            <goals>\r\n                <goal>compile<\/goal>\r\n                <goal>testCompile<\/goal>\r\n            <\/goals>\r\n        <\/execution>\r\n    <\/executions>\r\n    <dependencies>\r\n        <dependency>\r\n            <groupid>org.codehaus.gmaven.runtime<\/groupId>\r\n            <artifactid>gmaven-runtime-2.0<\/artifactId>\r\n            <version>${gmaven-plugin.version}<\/version>\r\n            <exclusions>\r\n                <exclusion>\r\n                    <groupid>org.codehaus.groovy<\/groupId>\r\n                    <artifactid>groovy-all<\/artifactId>\r\n                <\/exclusion>\r\n            <\/exclusions>\r\n        <\/dependency>\r\n        <dependency>\r\n            <groupid>org.codehaus.groovy<\/groupId>\r\n            <artifactid>groovy-all<\/artifactId>\r\n            <version>${groovy.version}<\/version>\r\n        <\/dependency>\r\n    <\/dependencies>\r\n<\/plugin>\r\n<\/pre>\n<div style=\"text-align: justify\">With these configuration we can use spock and write our first specifications. But there is one issue: default settings for maven-surefire plugin demand that test classes must end with \u201c..Test\u201d postfix, which is ok when we want to use such naming scheme for our spock tests. But if we want to name them like CommentSpec.groovy or whatever with \u201c..Spec\u201d ending (what in my opinion is much more readable) we need to make little change in surefire plugin configuration:<\/div>\n<div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\"><plugin>\r\n    <groupid>org.apache.maven.plugins\r\n    <\/groupId>\r\n    <artifactid>maven-surefire-plugin\r\n    <\/artifactId>\r\n    <version>2.15<\/version>\r\n    <configuration>\r\n        <includes>\r\n            <include>**\/*Test.java<\/include>\r\n            <include>**\/*Spec.java<\/include>\r\n        <\/includes>\r\n    <\/configuration>\r\n<\/plugin><\/pre>\n<\/div>\n<div style=\"text-align: justify\">As you can see there is a little trick ;) We add include directive for standard Java JUnit test ending with \u201c..Test\u201d postfix, but there is also an entry for spock test ending with \u201c..Spec\u201d. And there is a trick: <b>we must write \u201c**\/*Spec.java\u201d, not \u201c**\/*Spec.groovy\u201d<\/b>, otherwise Maven will not run spock tests (which is strange and I\u2019ve spent some time to figure out why Maven can\u2019t run my specs). <b> <\/b><b>Little update:<\/b>\u00a0instead of \u201c<b>*.java<\/b>\u201d postfix for both types of tests we can write \u201c*<b>.class<\/b>\u201d what is in my opinion more readable and clean:<\/div>\n<div style=\"text-align: justify\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\"><include>**\/*Test.class<\/include>\r\n<include>**\/*Spec.class<\/include>\r\n<\/pre>\n<p>(thanks to <b>Tomek P\u0119ksa <\/b>for pointing this out!)<\/p>\n<\/div>\n<div style=\"text-align: justify\">With such configuration, we can write either traditional JUnit test and put them in src\/test\/java directory or groovy spock specifications and place them in src\/test\/groovy. And both will work together just fine :) In one of my next posts I\u2019ll write something about using spock and its mocking abilities in practice, so stay in tune.<\/div>\n","protected":false},"excerpt":{"rendered":"Few months ago I&#8217;ve came across Groovy &#8211; powerful language for JVM platform which combines the power of Java with abilities typical for scripting languages (dynamic typing, metaprogramming).\n\nTogether with Groovy I&#8217;ve discovered spock framework (https:\/\/code.google.com\/p\/spock\/) &#8211; specification framework for Groovy (of course you can test Java classes too!). But spock is not only test\/specification framework &#8211; it also contains powerful mocking tools.\n\nEven though spock is dedicated for Groovy there is no problem with using it for Java classes tests. In this post I&#8217;m going to describe how to configure Maven project to build and run spock specifications together with traditional JUnit tests.\n\n\nFirstly, we need to prepare pom.xml and add necessary dependencies and plugins.\n\nTwo obligatory libraries are:\n&lt;dependency&gt;    &lt;groupid&gt;org.spockframework&lt;\/groupId&gt;    &lt;artifactid&gt;spock-core&lt;\/artifactId&gt;    &lt;version&gt;0.7-groovy-2.0&lt;\/version&gt;    &lt;scope&gt;test&lt;\/scope&gt;&lt;\/dependency&gt;&lt;dependency&gt;    &lt;groupid&gt;org.codehaus.groovy&lt;\/groupId&gt;    &lt;artifactid&gt;groovy-all&lt;\/artifactId&gt;    &lt;version&gt;${groovy.version}&lt;\/version&gt;    &lt;scope&gt;test&lt;\/scope&gt;&lt;\/dependency&gt;\n Where groovy.version is property defined in pom.xml for more convenient use and easy version change, just like this: \n&lt;properties&gt;    &lt;gmaven-plugin.version&gt;1.4&lt;\/gmaven-plugin.version&gt;    &lt;groovy.version&gt;2.1.5&lt;\/groovy.version&gt;&lt;\/properties&gt;\n\nI&#8217;ve added property for gmaven-plugin version for the same reason ;) \n\nBesides these two dependencies, we can use few additional ones providing extra functionality:\n\ncglib &#8211; for class mocking\nobjenesis &#8211; enables mocking classes without default constructor\n\nTo add them to the project put these lines in &lt;dependencies&gt; section of pom.xml:\n&lt;dependency&gt;    &lt;groupid&gt;cglib&lt;\/groupId&gt;    &lt;artifactid&gt;cglib-nodep&lt;\/artifactId&gt;    &lt;version&gt;3.0&lt;\/version&gt;    &lt;scope&gt;test&lt;\/scope&gt;&lt;\/dependency&gt;&lt;dependency&gt;    &lt;groupid&gt;org.objenesis&lt;\/groupId&gt;    &lt;artifactid&gt;objenesis&lt;\/artifactId&gt;    &lt;version&gt;1.3&lt;\/version&gt;    &lt;scope&gt;test&lt;\/scope&gt;&lt;\/dependency&gt;\n\nAnd that&#8217;s all for dependencies section. Now we will focus on plugins necessary to compile Groovy classes. We need to add gmaven-plugin with gmaven-runtime-2.0 dependency in plugins section:\n&lt;plugin&gt;    &lt;groupid&gt;org.codehaus.gmaven&lt;\/groupId&gt;    &lt;artifactid&gt;gmaven-plugin&lt;\/artifactId&gt;    &lt;version&gt;${gmaven-plugin.version}&lt;\/version&gt;    &lt;configuration&gt;        &lt;providerselection&gt;2.0&lt;\/providerSelection&gt;    &lt;\/configuration&gt;    &lt;executions&gt;        &lt;execution&gt;            &lt;goals&gt;                &lt;goal&gt;compile&lt;\/goal&gt;                &lt;goal&gt;testCompile&lt;\/goal&gt;            &lt;\/goals&gt;        &lt;\/execution&gt;    &lt;\/executions&gt;    &lt;dependencies&gt;        &lt;dependency&gt;            &lt;groupid&gt;org.codehaus.gmaven.runtime&lt;\/groupId&gt;            &lt;artifactid&gt;gmaven-runtime-2.0&lt;\/artifactId&gt;            &lt;version&gt;${gmaven-plugin.version}&lt;\/version&gt;            &lt;exclusions&gt;                &lt;exclusion&gt;                    &lt;groupid&gt;org.codehaus.groovy&lt;\/groupId&gt;                    &lt;artifactid&gt;groovy-all&lt;\/artifactId&gt;                &lt;\/exclusion&gt;            &lt;\/exclusions&gt;        &lt;\/dependency&gt;        &lt;dependency&gt;            &lt;groupid&gt;org.codehaus.groovy&lt;\/groupId&gt;            &lt;artifactid&gt;groovy-all&lt;\/artifactId&gt;            &lt;version&gt;${groovy.version}&lt;\/version&gt;        &lt;\/dependency&gt;    &lt;\/dependencies&gt;&lt;\/plugin&gt;\n\nWith these configuration we can use spock and write our first specifications. But there is one issue: default settings for maven-surefire plugin demand that test classes must end with &#8220;..Test&#8221; postfix, which is ok when we want to use such naming scheme for our spock tests. But if we want to name them like CommentSpec.groovy or whatever with &#8220;..Spec&#8221; ending (what in my opinion is much more readable) we need to make little change in surefire plugin configuration:\n&lt;plugin&gt;    &lt;groupid&gt;org.apache.maven.plugins&lt;\/groupId&gt;    &lt;artifactid&gt;maven-surefire-plugin&lt;\/artifactId&gt;    &lt;version&gt;2.15&lt;\/version&gt;    &lt;configuration&gt;        &lt;includes&gt;            &lt;include&gt;**\/*Test.java&lt;\/include&gt;            &lt;include&gt;**\/*Spec.java&lt;\/include&gt;        &lt;\/includes&gt;    &lt;\/configuration&gt;&lt;\/plugin&gt;\n\nAs you can see there is a little trick ;) We add include directive for standard Java JUnit test ending with &#8220;..Test&#8221; postfix, but there is also an entry for spock test ending with &#8220;..Spec&#8221;. And there is a trick: we must write &#8220;**\/*Spec.java&#8221;, not &#8220;**\/*Spec.groovy&#8221;, otherwise Maven will not run spock tests (which is strange and I&#8217;ve spent some time to figure out why Maven can&#8217;t run my specs).Little update:&nbsp;instead of &#8220;*.java&#8221; postfix for both types of tests we can write &#8220;*.class&#8221; what is in my opinion more readable and clean:\n&lt;include&gt;**\/*Test.class&lt;\/include&gt;&lt;include&gt;**\/*Spec.class&lt;\/include&gt;\n(thanks to Tomek P&#281;ksa for pointing this out!)\n\nWith such configuration, we can write either traditional JUnit test and put them in src\/test\/java directory or groovy spock specifications and place them in src\/test\/groovy. And both will work together just fine :) In one of my next posts I&#8217;ll write something about using spock and its mocking abilities in practice, so stay in tune.\n\n","protected":false},"author":48,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[50,68,90,411,30],"class_list":{"0":"post-12673","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-development-design","7":"tag-groovy","8":"tag-java","9":"tag-maven","10":"tag-spock","11":"tag-testing"},"_links":{"self":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12673","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=12673"}],"version-history":[{"count":6,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12673\/revisions"}],"predecessor-version":[{"id":14751,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/posts\/12673\/revisions\/14751"}],"wp:attachment":[{"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/media?parent=12673"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/categories?post=12673"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/touk.pl\/blog\/wp-json\/wp\/v2\/tags?post=12673"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}