{"id":74221,"date":"2018-03-01T07:00:58","date_gmt":"2018-03-01T05:00:58","guid":{"rendered":"https:\/\/www.javacodegeeks.com\/?p=74221"},"modified":"2018-02-28T10:52:58","modified_gmt":"2018-02-28T08:52:58","slug":"testing-code-spock","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2018\/03\/testing-code-spock.html","title":{"rendered":"Testing your code with Spock"},"content":{"rendered":"<p><a href=\"http:\/\/spockframework.org\/\">Spock<\/a> is a testing and specification framework for Java and Groovy applications.\u00a0 Spock is:<\/p>\n<ul>\n<li>Extremely expressive<\/li>\n<li>Facilitates the Given \/ When \/ Then <a href=\"https:\/\/martinfowler.com\/bliki\/GivenWhenThen.html\">syntax<\/a> for your tests<\/li>\n<li>compatible with most IDEs and CI Servers.<\/li>\n<\/ul>\n<p>Sounds interesting? Well you can start playing with Spock very quickly by paying a quick visit to the\u00a0<a href=\"http:\/\/meetspock.appspot.com\/?id=9001\">Spock web console<\/a>.\u00a0 When you have a little test you like, you can publish it like I did for this little\u00a0<a href=\"http:\/\/meetspock.appspot.com\/script\/5100621019480064\">Hello World test<\/a>.<\/p>\n<p><figure id=\"attachment_74228\" aria-describedby=\"caption-attachment-74228\" style=\"width: 640px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-27-at-23.08.52.png\"><img decoding=\"async\" class=\"wp-image-74228 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-27-at-23.08.52.png\" alt=\"\" width=\"640\" height=\"458\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-27-at-23.08.52.png 640w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-27-at-23.08.52-300x215.png 300w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><figcaption id=\"caption-attachment-74228\" class=\"wp-caption-text\">HelloWorld in Spock<\/figcaption><\/figure><\/p>\n<p>Firstly, Spock tests are written in Groovy.\u00a0 That means, some boiler plate code that you have with Java goes away.\u00a0 There isThis Hello World test serves as a gentle introduction to some of the features of Spock.<\/p>\n<ul>\n<li><i>No need<\/i> to indicate the class is Public as it is by default.<\/li>\n<li><i>No need<\/i> to declare firstWord and lastWord as Strings<\/li>\n<li><i>No need <\/i>to explicitly invoke assert, as every line of code in the expect block gets that automatically.\u00a0 <i>Just make sure the lines in that block are boolean expression. So in this case, it is just an equality expression which will either be true or false.\u00a0 <\/i><\/li>\n<\/ul>\n<p>So less boiler plate code what next?\u00a0 Well you know those really long test names you get with JUnit tests, well instead of having to call this test,\u00a0<i>helloWorldIntroductionToSpockTest<\/i>() which is difficult to read, you can just use a String with spaces to name the test:\u00a0<i>Hello World introduction to Spock test<\/i>. This makes things much more readable.<\/p>\n<p>Thirdly, if I were to make a small change to the test and change\u00a0<i>firstWord<\/i> to &#8221;\u00a0<i>Hello1<\/i>&#8220;,\u00a0 the test will of course fail. But when I get a failure in Spock, I get the full context of the expression that is tested.\u00a0 I see the value of\u00a0<i>firstWord<\/i>, the value of\u00a0<i>secondWord<\/i> and the value after the concatenation this makes it much quicker to diagnose problems when tests fail.<\/p>\n<p><figure id=\"attachment_74229\" aria-describedby=\"caption-attachment-74229\" style=\"width: 640px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-27-at-20.23.28.png\"><img decoding=\"async\" class=\"wp-image-74229 size-full\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-27-at-20.23.28.png\" alt=\"\" width=\"640\" height=\"547\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-27-at-20.23.28.png 640w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-27-at-20.23.28-300x256.png 300w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/a><figcaption id=\"caption-attachment-74229\" class=\"wp-caption-text\">Spock shows the context of the failure<\/figcaption><\/figure><\/p>\n<p>Mocking and StubbingNot bad for an introduction.\u00a0 Let&#8217;s now have a look at more features.<\/p>\n<p>Mocking and Stubbing are much more powerful in JUnit (<i>and various add on&#8217;s<\/i>). But, it is not only super powerful in Spock, it is also very terse, keeping your test code very neat and easy to read.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>Suppose we want to Stub a class called\u00a0<i>PaymentCalculator<\/i> in our test, more specifically a method,\u00a0<i>calculate(Product product, Integer count).<\/i>\u00a0\u00a0 In the stubbed version we want to return the count multiplied by 10 irrespective of the value of product. \u00a0 In Spock we achieve this by:<\/p>\n<pre class=\"brush:java\">PaymentCalculator paymentCalculator = Stub(PaymentCalculator)\r\npaymentCalculator.calculate(_, _) &gt;&gt; {p, c -&gt; c * 10}<\/pre>\n<p>If you haven&#8217;t realised how short and neat this is, well then get yourself a\u00a0<a href=\"https:\/\/www.bewleys.com\/ie\/coffee-project\/\">coffee<\/a>.\u00a0 If you have realised well you can still have a coffer but consider these points:<\/p>\n<ol>\n<li>The underscores in the calculate mean for all values<\/li>\n<li>On the right hand side we see a Groovy Closure. For now, think of this as an anonymous method with two inputs. p for the product, c for count. We don&#8217;t have to type them. That&#8217;s just more boiler plate code gone.<\/li>\n<li>The closure will always return the count time 10.\u00a0 We don&#8217;t need a <i>return<\/i> statement.\u00a0 The value of the last expression is always returned. Again, this means less boiler plate code.\u00a0 When stubbing becomes this easy and neat, it means you can really focus on the test &#8211; cool.<\/li>\n<\/ol>\n<h2>Parameterised Tests<\/h2>\n<p>The best way to explain this is by example.<\/p>\n<pre class=\"brush:java\">@Unroll\r\ndef \"Check that the rugby player #player who has Irish status #isIrish plays for Ireland\"(String player, Boolean isIrish) {\r\n    given:\"An instance of Rugby player validator\"\r\n    RugbyPlayerValidator rugbyPlayerValidator = new RugbyPlayerValidator()\r\n\r\n    expect:\r\n    rugbyPlayerValidator.isIrish(player)  == isIrish\r\n\r\n    where:\r\n    player               ||  isIrish\r\n    \"Johny Sexton\"       ||  true\r\n    \"Stuart Hogg\"        ||  false\r\n    \"Conor Murray\"       ||  true\r\n    \"George North\"       ||  false\r\n    \"Jack Nowell\"        ||  true\r\n\r\n}<\/pre>\n<p>In the this parameterised test we see the following:<\/p>\n<ol>\n<li>The test is parameterised, we this in the test signature and in the <i>where<\/i> block.<\/li>\n<li>There is one input parameter player and one output parameter &#8211; which corresponds to an expected value.<\/li>\n<li>The test is parameterised five times.\u00a0 The input parameters are on the left, output on the right. It is, of course, possible to have more of either, in this test we just have one of each.<\/li>\n<li>The @Unroll annotation will mean that if the test fails, the values of all parameters will be outputted. The message will substitute the details of player into #player and the details of the Irish status substituted into #isIrish. So for example, &#8220;<i>Checks that the rugby player Jack Nowell who has Irish status true plays for Ireland<\/i>&#8220;<\/li>\n<\/ol>\n<p>Again, this makes it much quicker to narrow in on bugs. Is the test wrong or is the code wrong? That becomes a question that can be answered faster.\u00a0 In this case the test is wrong.<\/p>\n<h2>All the benefits of Groovy<\/h2>\n<p>What else? Well another major benefit is all the benefits of Groovy.\u00a0 For example, suppose you are testing an API that returns JSON or XML. Groovy is brilliant for parsing XML and JSON. Suppose we have an API that returns information about sports players in XML format. The format varies, but only slightly, depending on the sport they play:<\/p>\n<pre class=\" brush:xml\">Joey Carberry\r\n   &lt;details&gt;\r\n &lt;rugbysummarycategory&gt;\r\n  &lt;players&gt;\r\n    &lt;player&gt;Joey Carberry&lt;\/player&gt;\r\n    &lt;player&gt;Teddy Thomas&lt;\/player&gt;\r\n  &lt;\/players&gt;\r\n&lt;\/rugbysummarycategory&gt;\r\n&lt;\/details&gt;\r\n \r\n&lt;details&gt;\r\n &lt;footballsummarycategory&gt;\r\n   &lt;players&gt;\r\n     &lt;player&gt;Lionel Messi&lt;\/player&gt;\r\n     &lt;player&gt;Cristiano Ronaldo&lt;\/player&gt;\r\n   &lt;\/players&gt;\r\n &lt;\/footballsummarycategory&gt;\r\n&lt;\/details&gt;<\/pre>\n<p>We want to just invoke this API and then parse out the players irrespective of the sport. We can parse this polymorphically very simply in Groovy.<\/p>\n<pre class=\"brush:java\">def rootNode = new XmlSlurper().parseText(xml)\r\nList players = rootNode.'*'.Players.Player*.text()<\/pre>\n<p>Some key points:<\/p>\n<ol>\n<li>The power of dynamic typing is immediate. The expression can be dynamically invoked on the rootNode. No verbose, complex XPath expression needed.<\/li>\n<li>The &#8216;*&#8217;, is like a wildcard. That will cover both RugbySummaryCategory and FootballSummaryCategory.<\/li>\n<li>The Player*, means for all Player elements. So no silly verbose for loop needed here<\/li>\n<li>The text() expression just pulls out the values of the text between the respective Player elements. So why now have a list all players and can simple do: players.size() == 4 Remember, no need for the assert.<\/li>\n<\/ol>\n<p>Suppose we want to check the players names. Well in this case we don&#8217;t care about order, so make more sense to convert the list to a Set and then check. Simple.<\/p>\n<pre class=\"brush:bash\">players as Set = [\"Joey Carberry\", \"Teddy Thomas\", \"Lionel Messi\", Cristiano Ranaldo\"] as Set<\/pre>\n<p>This will convert both list to a Set which means then order checking is gone and it is just a Set comparison. There&#8217;s a tonne more Groovy advantages we can take advantage of. But the beauty is, we don&#8217;t actually have to.<br \/>\n<b><i>All Java code is also valid in a Groovy class<\/i><\/b>. The same hold trues for Spock. This means there is no steep learner curve for anyone from a Java background. They can code pure Java and then get some Groovy tips from code reviews etc.<\/p>\n<h2>Powerful annotations<\/h2>\n<p>Spock also has a range of powerful annotations for your tests. Again, we see the power of Groovy here as we can pass a closure to these annotations. For example:<\/p>\n<pre class=\"brush:java\">@IgnoreIf({System.getProperty(\"os.name\").contains(\"windows\")})\r\ndef \"I'll run anywhere except windows\"() {...}<\/pre>\n<p>Or just make your test fail if they take too long to execute<\/p>\n<pre class=\"brush:java\">@Timeout(value = 100, unit=TimeUnit.MILLISECONDS)\r\ndef \"I better be quick\"() {...}<\/pre>\n<p>So in summary Spock versus vanilla JUnit has the following advantages:<\/p>\n<ol>\n<li>Test Structure enforced. No more random asserts. Assertions can only be in designated parts of the code.<\/li>\n<li>Test code is much more readable.<\/li>\n<li>Much more information on the context of the failed test<\/li>\n<li>Can mock and stub with much less code<\/li>\n<li>Can leverage a pile of Groovy features to make code much less verbose<\/li>\n<li>Very powerful test parameterisation which can be done very neatly<\/li>\n<li>A range of powerful annotations.<\/li>\n<\/ol>\n<p>And one of the often forgotten points is that your project doesn&#8217;t have to be written in Groovy. You can keep it all in Java and leverage the static typing of Java for your production code and use the power and speed of Groovy for your test code.<\/p>\n<p>Until the next time take care of yourselves.<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td>Published on Java Code Geeks with permission by Alex Staveley, partner at our <a href=\"http:\/\/www.javacodegeeks.com\/join-us\/jcg\/\" target=\"_blank\" rel=\"noopener\">JCG program<\/a>. See the original article here: <a href=\"http:\/\/dublintech.blogspot.com\/2018\/02\/testing-your-code-with-spock.html\" target=\"_blank\" rel=\"noopener\">Testing your code with Spock<\/a><\/p>\n<p>Opinions expressed by Java Code Geeks contributors are their own.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Spock is a testing and specification framework for Java and Groovy applications.\u00a0 Spock is: Extremely expressive Facilitates the Given \/ When \/ Then syntax for your tests compatible with most IDEs and CI Servers. Sounds interesting? Well you can start playing with Spock very quickly by paying a quick visit to the\u00a0Spock web console.\u00a0 When &hellip;<\/p>\n","protected":false},"author":78,"featured_media":112,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[1004],"class_list":["post-74221","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-spock"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Testing your code with Spock - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Spock is a testing and specification framework for Java and Groovy applications.\u00a0 Spock is: Extremely expressive Facilitates the Given \/ When \/ Then\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2018\/03\/testing-code-spock.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Testing your code with Spock - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Spock is a testing and specification framework for Java and Groovy applications.\u00a0 Spock is: Extremely expressive Facilitates the Given \/ When \/ Then\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2018\/03\/testing-code-spock.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2018-03-01T05:00:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-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=\"Alex Staveley\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@http:\/\/twitter.com\/dublintech\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Alex Staveley\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/testing-code-spock.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/testing-code-spock.html\"},\"author\":{\"name\":\"Alex Staveley\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/c36bb99e6c62bd244448ff5b88a5dbb0\"},\"headline\":\"Testing your code with Spock\",\"datePublished\":\"2018-03-01T05:00:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/testing-code-spock.html\"},\"wordCount\":1265,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/testing-code-spock.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"keywords\":[\"Spock\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/testing-code-spock.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/testing-code-spock.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/testing-code-spock.html\",\"name\":\"Testing your code with Spock - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/testing-code-spock.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/testing-code-spock.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"datePublished\":\"2018-03-01T05:00:58+00:00\",\"description\":\"Spock is a testing and specification framework for Java and Groovy applications.\u00a0 Spock is: Extremely expressive Facilitates the Given \\\/ When \\\/ Then\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/testing-code-spock.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/testing-code-spock.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/testing-code-spock.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/enterprise-java-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"java-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2018\\\/03\\\/testing-code-spock.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Testing your code with Spock\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/c36bb99e6c62bd244448ff5b88a5dbb0\",\"name\":\"Alex Staveley\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c203a6c5693c1d76090c0b37893b409d89a4285f1fee2a545f80068d27a79cd7?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c203a6c5693c1d76090c0b37893b409d89a4285f1fee2a545f80068d27a79cd7?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/c203a6c5693c1d76090c0b37893b409d89a4285f1fee2a545f80068d27a79cd7?s=96&d=mm&r=g\",\"caption\":\"Alex Staveley\"},\"sameAs\":[\"http:\\\/\\\/www.dublintech.blogspot.com\\\/\",\"http:\\\/\\\/www.linkedin.com\\\/pub\\\/alex-staveley\\\/14\\\/977\\\/4a8\",\"https:\\\/\\\/x.com\\\/http:\\\/\\\/twitter.com\\\/dublintech\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/Alex-Staveley\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Testing your code with Spock - Java Code Geeks","description":"Spock is a testing and specification framework for Java and Groovy applications.\u00a0 Spock is: Extremely expressive Facilitates the Given \/ When \/ Then","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:\/\/www.javacodegeeks.com\/2018\/03\/testing-code-spock.html","og_locale":"en_US","og_type":"article","og_title":"Testing your code with Spock - Java Code Geeks","og_description":"Spock is a testing and specification framework for Java and Groovy applications.\u00a0 Spock is: Extremely expressive Facilitates the Given \/ When \/ Then","og_url":"https:\/\/www.javacodegeeks.com\/2018\/03\/testing-code-spock.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2018-03-01T05:00:58+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","type":"image\/jpeg"}],"author":"Alex Staveley","twitter_card":"summary_large_image","twitter_creator":"@http:\/\/twitter.com\/dublintech","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Alex Staveley","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/testing-code-spock.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/testing-code-spock.html"},"author":{"name":"Alex Staveley","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/c36bb99e6c62bd244448ff5b88a5dbb0"},"headline":"Testing your code with Spock","datePublished":"2018-03-01T05:00:58+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/testing-code-spock.html"},"wordCount":1265,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/testing-code-spock.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","keywords":["Spock"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2018\/03\/testing-code-spock.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/testing-code-spock.html","url":"https:\/\/www.javacodegeeks.com\/2018\/03\/testing-code-spock.html","name":"Testing your code with Spock - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/testing-code-spock.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/testing-code-spock.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","datePublished":"2018-03-01T05:00:58+00:00","description":"Spock is a testing and specification framework for Java and Groovy applications.\u00a0 Spock is: Extremely expressive Facilitates the Given \/ When \/ Then","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/testing-code-spock.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2018\/03\/testing-code-spock.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/testing-code-spock.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/enterprise-java-logo.jpg","width":150,"height":150,"caption":"java-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2018\/03\/testing-code-spock.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Testing your code with Spock"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/c36bb99e6c62bd244448ff5b88a5dbb0","name":"Alex Staveley","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c203a6c5693c1d76090c0b37893b409d89a4285f1fee2a545f80068d27a79cd7?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c203a6c5693c1d76090c0b37893b409d89a4285f1fee2a545f80068d27a79cd7?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c203a6c5693c1d76090c0b37893b409d89a4285f1fee2a545f80068d27a79cd7?s=96&d=mm&r=g","caption":"Alex Staveley"},"sameAs":["http:\/\/www.dublintech.blogspot.com\/","http:\/\/www.linkedin.com\/pub\/alex-staveley\/14\/977\/4a8","https:\/\/x.com\/http:\/\/twitter.com\/dublintech"],"url":"https:\/\/www.javacodegeeks.com\/author\/Alex-Staveley"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/74221","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/78"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=74221"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/74221\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/112"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=74221"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=74221"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=74221"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}