{"id":832,"date":"2017-04-20T12:19:09","date_gmt":"2017-04-20T10:19:09","guid":{"rendered":"https:\/\/zaven.co\/blog\/?p=832"},"modified":"2025-04-08T19:55:09","modified_gmt":"2025-04-08T17:55:09","slug":"bdd-frameworks-part-3","status":"publish","type":"post","link":"https:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/","title":{"rendered":"BDD frameworks (part 3)"},"content":{"rendered":"<p>In this series of articles, <b>I am presenting selected<\/b> <strong><abbr title=\"Behavior Driven Development\">BDD<\/abbr> frameworks<\/strong>. In this part, I will talk about Spock \u2013 a unit testing framework.<!--more--><\/p>\n<h1>Writing unit tests with Spock framework<\/h1>\n<p><em>Spock<\/em> is a framework in which the test are written with a language called <em>Groovy<\/em>. Here the natural language layer is not separated from the logic that is responsible for the test activity, but possesses the keywords <code>given<\/code>, <code>when<\/code>, and <code>then<\/code>, which can help you understand the test.<\/p>\n<h2>Test definition in Spock<\/h2>\n<p>In the Spock framework, a testing class must inherit from the Specification class:<\/p>\n<pre><code class=\"language-groovy\">\nclass CalculateAreaTest extends Specification {\n} <\/code><\/pre>\n<p>The test begins with the phrase <code>def<\/code>:<\/p>\n<pre><code class=\"language-groovy\">\ndef \"Testing methods area of circle\"() {\n    given: \"init\"\n    def calculateArea = new CalculateArea()\n    def radius = 2\n    def expectedValue = 12.566370614359172\n    when: \"Calculation of the circle\"\n    def result = calculateArea.areaOfCircle(radius)\n    then: \"Check result\"\n    result == expectedValue\n}\n\n<\/code><\/pre>\n<h2>Data Table \u2013 testing several values<\/h2>\n<p>An interesting option we can use here is the Data Table. Input and output of the test can be provided in the table. So that we can simultaneously check several values in one test.<\/p>\n<pre><code class=\"language-groovy\">\ndef \"Testing methods area of circle - Data Tables\"(int radius, double expectedValue) {\n    given: \"init\"\n    def calculateArea = new CalculateArea()\n    expect: \"Checking the value from the table\"\n    calculateArea.areaOfCircle(radius) == expectedValue\n    where: \"Values for the variables\"\n    radius | expectedValue\n         1 | 3.141592653589793\n         7 | 153.93804002589985\n         0 | 0\n}\n\n<\/code><\/pre>\n<h2>Testing the calling of a method<\/h2>\n<p>Another useful feature of Spock is to test whether a particular method has been performed. This function may be useful to us when the method we are testing comes from some interface and we have to check if it has been called a certain number of times.<\/p>\n<pre><code class=\"language-groovy\">\ndef \"Testing methods area of square\"() {\n    given: \"init\"\n    def calculateArea = new CalculateArea()\n    def valueX = 2\n    def valueY = 2\n    def listener = Mock(OnSquareListener)\n    def expectedValue = 4\n    when: \"Calculation of the square\"\n    calculateArea.areaOfSquare(valueX, valueY, listener)\n    then: \"Verify that appropriate methods are called\"\n        1 * listener.onSuccess(expectedValue)\n        0 * listener.onFailed()\n}\n<\/code><\/pre>\n<p>In the code <code>\"quantity\"<\/code> determines how many times a given method should be called: <code>quantity * object.method().<\/code><\/p>\n<h2>Additional elements<\/h2>\n<p>Sometimes we need additional Android elements. Such as context, for testing. For such needs, we can use additional annotations:<\/p>\n<ul>\n<li><code>@UseActivity(MyActivity)<\/code><\/li>\n<li><code>@UseApplication(MyApplication)<\/code><\/li>\n<li><code>@WithContext<\/code><\/li>\n<\/ul>\n<p>Example use of annotations:<\/p>\n<pre><code class=\"language-groovy\">\nclass ContextTest extends Specification {\n    @WithContext\n    def context\n    def \"Test context\"() {\n        expect:\n        context != null\n    }\n}\n<\/code><\/pre>\n<p>It should be remembered that when we want to use Android elements, a test needs to be placed in the directory: app\/src\/androidTest\/groovy.<\/p>\n<p>However, when we do not need these elements, we put the files in the directory: app\/src\/test\/groovy. All files have the extension <code>.groovy<\/code>.<\/p>\n<p>The installation of the Android elements proceeds as follows:<\/p>\n<pre><code class=\"language-gradle\">\nbuildscript {\n  repositories {\n    jcenter()\n  }\n\n  dependencies {\n     classpath 'com.android.tools.build:gradle:2.2.2'\n     classpath 'org.codehaus.groovy:groovy-android-gradle-plugin:1.1.0'\n  }\n}\n<\/code><\/pre>\n<p>Next:<\/p>\n<pre><code class=\"language-gradle\">\napply plugin: 'com.android.application'\napply plugin: 'groovyx.android'\n\n\nandroid {\n\n  defaultConfig {\n    ...\n    testInstrumentationRunner \"android.support.test.runner.AndroidJUnitRunner\"\n  }\n\n  packagingOptions {\n    exclude 'META-INF\/services\/org.codehaus.groovy.transform.ASTTransformation'\n    exclude 'LICENSE.txt'\n  }\n}\n\ndependencies {\n\/\/spock without components Android\n    testCompile 'org.spockframework:spock-core:1.0-groovy-2.4'\n\n\/\/spock with elements of Android\n    androidTestCompile 'org.codehaus.groovy:groovy:2.4.4:grooid'\n    androidTestCompile('org.spockframework:spock-core:1.0-groovy-2.4') {\n        exclude group: 'org.codehaus.groovy'\n        exclude group: 'junit'\n    }\n    androidTestCompile 'com.andrewreitz:spock-android:1.2.2'\n    androidTestCompile 'com.crittercism.dexmaker:dexmaker:1.4'\n}\n<\/code><\/pre>\n<h1>BDD frameworks<\/h1>\n<p><strong>Behavior-Driven Development<\/strong> <b>is a useful process for testing application functionality.<\/b><\/p>\n<p>Thanks to BDD testing tools like Calabash used with the Cucumber framework or Green Coffee based on Gherkin, <b>everyone involved in the project can easily verify the views of the Android app being designed. And check if the functionalities fulfill the criteria<\/b> for acceptance that were stated in the requirements.<\/p>\n<p>The Spock framework helps Unit testing be performed efficiently. Now the communication between programmer, manager and customer will be clear, and automated software testing will end in success!<\/p>\n<p>Check our previous article: <a href=\"https:\/\/zaven.co\/blog\/bdd-frameworks-part-2\/\">BDD frameworks (part 2)<\/a>.<\/p>\n<hr>\n<h3>You can find the source code on my <a href=\"https:\/\/github.com\/woszos\/bdd-android-frameworks\" target=\"_blank\" rel=\"noopener noreferrer\">GitHub<\/a>.<\/h3>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>In this series of articles, I am presenting selected BDD frameworks. In this part, I will talk about Spock \u2013 a unit testing framework.<\/p>\n","protected":false},"author":13,"featured_media":835,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59,56,60,5],"tags":[83,77,76,90,80,84,82,79],"class_list":["post-832","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-agile-methodology","category-android-development","category-project-management","category-tutorials","tag-android-app","tag-app-testing","tag-bdd","tag-bdd-frameworks","tag-best-driven-development","tag-mobile-apps","tag-tools","tag-unit-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Behavior-Driven Development. BDD framework (part 3) | Zaven Blog<\/title>\n<meta name=\"description\" content=\"In this series of articles, I am presenting selected BDD testing tools. In this part, I will talk about Spock \u2013 a unit testing BDD frameworks.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Behavior-Driven Development. BDD framework (part 3) | Zaven Blog\" \/>\n<meta property=\"og:description\" content=\"In this series of articles, I am presenting selected BDD testing tools. In this part, I will talk about Spock \u2013 a unit testing BDD frameworks.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/\" \/>\n<meta property=\"og:site_name\" content=\"Zaven Blog\" \/>\n<meta property=\"article:published_time\" content=\"2017-04-20T10:19:09+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-04-08T17:55:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2017\/04\/mniejsze.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"2783\" \/>\n\t<meta property=\"og:image:height\" content=\"1857\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Wojciech Sz\u00f3stak\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Wojciech Sz\u00f3stak\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/\",\"url\":\"https:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/\",\"name\":\"Behavior-Driven Development. BDD framework (part 3) | Zaven Blog\",\"isPartOf\":{\"@id\":\"https:\/\/zaven.co\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2017\/04\/mniejsze.jpg\",\"datePublished\":\"2017-04-20T10:19:09+00:00\",\"dateModified\":\"2025-04-08T17:55:09+00:00\",\"author\":{\"@id\":\"https:\/\/zaven.co\/blog\/#\/schema\/person\/6a25629cbfe7aef5ecf9eacd3b4757b6\"},\"description\":\"In this series of articles, I am presenting selected BDD testing tools. In this part, I will talk about Spock \u2013 a unit testing BDD frameworks.\",\"breadcrumb\":{\"@id\":\"https:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/#primaryimage\",\"url\":\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2017\/04\/mniejsze.jpg\",\"contentUrl\":\"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2017\/04\/mniejsze.jpg\",\"width\":2783,\"height\":1857,\"caption\":\"BDD frameworks\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/zaven.co\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"BDD frameworks (part 3)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/zaven.co\/blog\/#website\",\"url\":\"https:\/\/zaven.co\/blog\/\",\"name\":\"Zaven Blog\",\"description\":\"Software development blog. Generative AI, web &amp; mobile applications.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/zaven.co\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/zaven.co\/blog\/#\/schema\/person\/6a25629cbfe7aef5ecf9eacd3b4757b6\",\"name\":\"Wojciech Sz\u00f3stak\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/zaven.co\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cbebf8b35feebd842c320b56f1edbf1a?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cbebf8b35feebd842c320b56f1edbf1a?s=96&d=mm&r=g\",\"caption\":\"Wojciech Sz\u00f3stak\"},\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/wojciech-szstak-513020113\/\"],\"url\":\"https:\/\/zaven.co\/blog\/author\/wojciech\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Behavior-Driven Development. BDD framework (part 3) | Zaven Blog","description":"In this series of articles, I am presenting selected BDD testing tools. In this part, I will talk about Spock \u2013 a unit testing BDD frameworks.","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:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/","og_locale":"en_US","og_type":"article","og_title":"Behavior-Driven Development. BDD framework (part 3) | Zaven Blog","og_description":"In this series of articles, I am presenting selected BDD testing tools. In this part, I will talk about Spock \u2013 a unit testing BDD frameworks.","og_url":"https:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/","og_site_name":"Zaven Blog","article_published_time":"2017-04-20T10:19:09+00:00","article_modified_time":"2025-04-08T17:55:09+00:00","og_image":[{"width":2783,"height":1857,"url":"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2017\/04\/mniejsze.jpg","type":"image\/jpeg"}],"author":"Wojciech Sz\u00f3stak","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Wojciech Sz\u00f3stak","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/","url":"https:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/","name":"Behavior-Driven Development. BDD framework (part 3) | Zaven Blog","isPartOf":{"@id":"https:\/\/zaven.co\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/#primaryimage"},"image":{"@id":"https:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/#primaryimage"},"thumbnailUrl":"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2017\/04\/mniejsze.jpg","datePublished":"2017-04-20T10:19:09+00:00","dateModified":"2025-04-08T17:55:09+00:00","author":{"@id":"https:\/\/zaven.co\/blog\/#\/schema\/person\/6a25629cbfe7aef5ecf9eacd3b4757b6"},"description":"In this series of articles, I am presenting selected BDD testing tools. In this part, I will talk about Spock \u2013 a unit testing BDD frameworks.","breadcrumb":{"@id":"https:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/#primaryimage","url":"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2017\/04\/mniejsze.jpg","contentUrl":"https:\/\/zaven.co\/blog\/wp-content\/uploads\/2017\/04\/mniejsze.jpg","width":2783,"height":1857,"caption":"BDD frameworks"},{"@type":"BreadcrumbList","@id":"https:\/\/zaven.co\/blog\/bdd-frameworks-part-3\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/zaven.co\/blog\/"},{"@type":"ListItem","position":2,"name":"BDD frameworks (part 3)"}]},{"@type":"WebSite","@id":"https:\/\/zaven.co\/blog\/#website","url":"https:\/\/zaven.co\/blog\/","name":"Zaven Blog","description":"Software development blog. Generative AI, web &amp; mobile applications.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/zaven.co\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/zaven.co\/blog\/#\/schema\/person\/6a25629cbfe7aef5ecf9eacd3b4757b6","name":"Wojciech Sz\u00f3stak","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/zaven.co\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cbebf8b35feebd842c320b56f1edbf1a?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cbebf8b35feebd842c320b56f1edbf1a?s=96&d=mm&r=g","caption":"Wojciech Sz\u00f3stak"},"sameAs":["https:\/\/www.linkedin.com\/in\/wojciech-szstak-513020113\/"],"url":"https:\/\/zaven.co\/blog\/author\/wojciech\/"}]}},"_links":{"self":[{"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/posts\/832","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/comments?post=832"}],"version-history":[{"count":16,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/posts\/832\/revisions"}],"predecessor-version":[{"id":69771,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/posts\/832\/revisions\/69771"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/media\/835"}],"wp:attachment":[{"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/media?parent=832"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/categories?post=832"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zaven.co\/blog\/wp-json\/wp\/v2\/tags?post=832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}