{"id":109710,"date":"2021-01-29T10:00:49","date_gmt":"2021-01-29T09:00:49","guid":{"rendered":"https:\/\/blog.jetbrains.com\/?post_type=idea&#038;p=109710"},"modified":"2021-05-21T18:32:31","modified_gmt":"2021-05-21T17:32:31","slug":"tutorial-spock-part-1-getting-started","status":"publish","type":"idea","link":"https:\/\/blog.jetbrains.com\/en\/idea\/2021\/01\/tutorial-spock-part-1-getting-started","title":{"rendered":"Tutorial: Spock Part 1 &#8211; Getting started"},"content":{"rendered":"<p><a href=\"http:\/\/spockframework.org\" target=\"_blank\" rel=\"noopener\">Spock<\/a> is a really interesting framework for writing automated tests on the JVM. Spock tests are written in Groovy, but they can be used to test Java classes as well. Check out our 30 minute video tutorial on how to write tests using Spock, or follow along with this series of blog posts, which contains code samples and links to further information. Or do both!<\/p>\n<p><iframe loading=\"lazy\" title=\"IntelliJ IDEA. Writing Tests with Spock (2021)\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/i5Qu3qYOfsM?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/p>\n<p>In this post, Part 1 in the series, we&#8217;re going to cover just enough to get you started with a brand new project for writing Spock tests.<\/p>\n<p>Tutorial: Spock<\/p>\n<ul>\n<li><strong>Part 1 &#8211; Getting Started<\/strong><\/li>\n<li><a href=\"https:\/\/blog.jetbrains.com\/en\/idea\/2021\/02\/tutorial-spock-part-2-writing-tests\/\">Part 2 &#8211; Writing Tests<\/a><\/li>\n<li><a href=\"https:\/\/blog.jetbrains.com\/en\/idea\/2021\/02\/tutorial-spock-part-3-data-driven-testing\/\">Part 3 &#8211; Data Driven Testing<\/a><\/li>\n<li><a href=\"https:\/\/blog.jetbrains.com\/en\/idea\/2021\/02\/tutorial-spock-part-4-mocking-and-stubbing\/\">Part 4 &#8211; Mocking and Stubbing<\/a><\/li>\n<li><a href=\"https:\/\/blog.jetbrains.com\/en\/idea\/2021\/02\/tutorial-spock-part-5-other-useful-tips\/\">Part 5 &#8211; Other Useful Tips<\/a><\/li>\n<\/ul>\n<p><em>These blog posts cover the same material as the video. This provides an easy way for people to skim the content quickly if they prefer reading to watching, and to give the reader\/watcher code samples and links to additional information.<\/em><\/p>\n<p><!--more--><\/p>\n<h2>Creating a Project for Spock<\/h2>\n<p>Let&#8217;s start by creating a new project that&#8217;s going to contain Java project code, and use Spock to unit test that code.<\/p>\n<p>From the new project window, we can choose any of:<\/p>\n<ul>\n<li>Java<\/li>\n<li>Maven<\/li>\n<li>Gradle<\/li>\n<\/ul>\n<p>&#8230;from the left-hand options to create our JVM project. For this tutorial we&#8217;re going to use <a href=\"https:\/\/gradle.org\/\" target=\"_blank\" rel=\"noopener\">Gradle<\/a> as our build tool.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/01\/01-new-project.png\" alt=\"\" \/><\/p>\n<p>Spock is a <a href=\"https:\/\/groovy-lang.org\/\" target=\"_blank\" rel=\"noopener\">Groovy<\/a> testing framework, although we&#8217;re going to use it to test Java code, so we could select Groovy as an additional library to add right at the start. However, we can also add Groovy later on, which is what I want to show, so I&#8217;m not going to select it here.<\/p>\n<p>We&#8217;re using JDK 11 for this project, but there&#8217;s no functionality here that requires 11, you can use whichever version you&#8217;re comfortable with here.<\/p>\n<p>Call the project spock-tutorial, and save it to some useful location.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/01\/02-project-name.png\" alt=\"\" \/><\/p>\n<p>IntelliJ IDEA creates the files for the project and initialises the structure of the project. It&#8217;s using Gradle to build this skeleton project to make sure all dependencies are downloaded and set up.<\/p>\n<h2>Setting Up Dependencies<\/h2>\n<p>Now the basics of the project have been created, let&#8217;s take a closer look and set up the dependencies we need.<\/p>\n<p>IntelliJ IDEA adds JUnit 5 as the default testing framework, which is a logical choice, but we want to use Spock instead of JUnit. We can <a href=\"https:\/\/www.jetbrains.com\/help\/idea\/work-with-gradle-dependency-diagram.html#add_gradle_dependency\" target=\"_blank\" rel=\"noopener\">generate a new dependency<\/a> for the libraries we want using <strong>\u2318N<\/strong> \/ <strong>Alt+Insert<\/strong> in the <code>build.gradle<\/code> file and then choosing &#8220;Add Maven artifact dependency&#8221;.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/01\/03-spock-dependency.png\" alt=\"\" \/><\/p>\n<p>We can search for org.spockframework in the artifact search, and add the <a href=\"https:\/\/github.com\/spockframework\/spock\/releases\" target=\"_blank\" rel=\"noopener\">latest version<\/a> of the spock-core library. This is currently Spock 2.0 for Groovy 3. This is a test dependency so make sure the declaration is <code>testImplementation<\/code>.<\/p>\n<p>We also need a dependency on Groovy, since Spock tests are written in Groovy. Follow the same steps for adding a new Maven artifact dependency, search for Groovy and add a dependency on Groovy 3.0.8. For now, we&#8217;re only going to use Groovy for testing, so we&#8217;ll declare this as <code>testImplementation<\/code> too.<\/p>\n<p>You can delete the other JUnit dependencies since we don&#8217;t need them, your <code>build.gradle<\/code> dependencies should look like:<\/p>\n<pre><code class=\"language-groovy\">dependencies {\n    testImplementation 'org.spockframework:spock-core:2.0-groovy-3.0'\n    testImplementation 'org.codehaus.groovy:groovy-all:3.0.8'\n}<\/code><\/pre>\n<p>Since we&#8217;re using Groovy, we need to add the <a href=\"https:\/\/docs.gradle.org\/current\/userguide\/groovy_plugin.html\" target=\"_blank\" rel=\"noopener\">groovy plugin<\/a> to Gradle. When we&#8217;re using the Groovy plugin, we don&#8217;t need to specifically add the Java plugin as well, since all that functionality is included in the Groovy plugin. We might want to delete <code>java<\/code> plugin just to keep the noise down in our build file. We can keep the plugin to be explicit, or delete it to be more succinct, it&#8217;s really down to our own preferences.<\/p>\n<pre><code class=\"language-groovy\">plugins {\n    id 'groovy'\n}<\/code><\/pre>\n<p><a href=\"https:\/\/github.com\/trishagee\/spock-testing-demo\/blob\/1ced2b4d118a3bcb418f05c1470dbef665b8eee9\/build.gradle\" target=\"_blank\" rel=\"noopener\">Full build.gradle code<\/a><\/p>\n<p>Now we can load all the Gradle changes with <strong>\u21e7\u2318I<\/strong> (macOS), or <strong>Ctrl+Shift+O<\/strong> (Windows\/Linux), and IntelliJ IDEA will download any new dependencies, remove the old ones, and correctly configure the project.<\/p>\n<p>With a mixed language project like this one, one way to organise the files is to keep Java code in the <code>java<\/code> folder and to create a <code>groovy<\/code> folder for Groovy code.<\/p>\n<p>From the Project window, select the <code>test<\/code> folder and press <strong>\u2318N<\/strong> (macOS) or <strong>Alt+Insert<\/strong> (Windows\/Linux). Select &#8220;Directory&#8221;, and start typing &#8220;groovy&#8221; in the &#8220;New Directory&#8221; dialog. IntelliJ IDEA should suggest &#8220;groovy&#8221; in the dropdown because we&#8217;re using the Groovy plugin in Gradle.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/01\/04-groovy-dir.png\" alt=\"\" \/><\/p>\n<p><em><a href=\"https:\/\/youtu.be\/i5Qu3qYOfsM?t=8\" target=\"_blank\" rel=\"noopener\">View steps in video<\/a><\/em><\/p>\n<h2>A Simple Assertion<\/h2>\n<p>Let&#8217;s start by writing a very simple test, so we can see what Spock tests look like.<\/p>\n<p>From the Project window, we can use the shortcut <strong>\u2318N<\/strong> (macOS) or <strong>Alt+Insert<\/strong> (Windows\/Linux) to create a new file. Create a Groovy class, since Spock tests are Groovy classes:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/01\/05.png\" alt=\"\" \/><\/p>\n<p>Create the package and directory structure by typing the full package name before the class name.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/01\/06.png\" alt=\"\" \/><\/p>\n<p>Spock tests are often called Specifications, so call the test <code>ExampleSpecification<\/code>. Make sure this class extends Spock&#8217;s <a href=\"http:\/\/spockframework.org\/spock\/docs\/2.0\/all_in_one.html#_specification\" target=\"_blank\" rel=\"noopener\">Specification<\/a> class. We can get IntelliJ IDEA to generate test methods for Spock specifications using <strong>\u2318N<\/strong> (macOS) or <strong>Alt+Insert<\/strong> (Windows\/Linux) inside the class.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/01\/07.png\" alt=\"\" \/><\/p>\n<p>This can be especially useful when we&#8217;re not so familiar with writing Spock or groovy code, as IntelliJ IDEA will generate the basic structure that we need. The method is defined with the &#8220;def&#8221; keyword, and Spock&#8217;s method names can be Strings. This is extremely useful when we&#8217;re creating tests, as the text format gives us a lot of flexibility for describing exactly what we are specifying the behaviour should be.<\/p>\n<pre><code class=\"language-groovy\">class ExampleSpecification extends Specification {\n    def \"should be a simple assertion\"() {\n\n    }\n}<\/code><\/pre>\n<p>Spock tests don&#8217;t use an annotation or a specific test method name format to define which methods are tests. Instead, they use labels. We&#8217;ll see the different types of labels available to use throughout the tutorial, for this simplest test we&#8217;re going to use <code>expect<\/code>. This can be used for defining the simplest behaviour we expect to see.<\/p>\n<pre><code class=\"language-groovy\">def \"should be a simple assertion\"() {\n    expect:\n    1 == 1\n}<\/code><\/pre>\n<p>Spock also doesn&#8217;t use Assertions or an Assert keyword, at least not normally. Instead, you can use simple checks, like the double equals. The <code>should be a simple assertion<\/code> method specifies a simple expected behaviour &#8211; that the number 1 should be equal to 1. It&#8217;s not a realistic test case, but it shows the basics of a Spock test.<\/p>\n<p>Run this test using <strong>\u2303R<\/strong> (macOS) or <strong>Shift+F10<\/strong> (Windows\/Linux), or the green arrow in the gutter near the line numbers. This test should pass.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/01\/08.png\" alt=\"\" \/><\/p>\n<p>Now, change it so that it fails, it&#8217;s always helpful to see a test fail first to make sure it&#8217;s actually working and testing the right things. When a test fails, Spock displays a helpful error message which shows exactly what failed and why.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"\">Condition not satisfied:\n\n1 == 0\n  |\n  false<\/pre>\n<p>By default IntelliJ IDEA uses Gradle to run tests if it&#8217;s a Gradle project. This is normally what we want, as it means our IDE is using the same process to run tests as the continuous integration or build environment is using. In a simple project like this one, where the Gradle build isn&#8217;t doing anything extra like generating code or resources, it can be faster to use the IntelliJ IDEA test runner. We can <a href=\"https:\/\/www.jetbrains.com\/help\/idea\/gradle-settings.html\" target=\"_blank\" rel=\"noopener\">find this in the preferences<\/a> under Build, Execution, Deployment &gt; Gradle, and we can select IntelliJ IDEA to run the tests.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2021\/01\/09.png\" alt=\"\" \/><\/p>\n<p>Fix this test and rerun it to make sure it passes. You should see it&#8217;s not using Gradle to run it, there shouldn&#8217;t be any Gradle output in the test results window.<\/p>\n<p><em><a href=\"https:\/\/youtu.be\/i5Qu3qYOfsM?t=196\" target=\"_blank\" rel=\"noopener\">View steps in video<\/a><\/em><\/p>\n<h2>Conclusion<\/h2>\n<p>In this blog, we covered the first three steps in our Spock tutorials. Now you know how to:<\/p>\n<ul>\n<li>Set up your project to use Spock<\/li>\n<li>Create simple a Spock test<\/li>\n<\/ul>\n<p>Spock has much more to offer than this, stay tuned for further blog posts, watch the <a href=\"https:\/\/youtu.be\/i5Qu3qYOfsM\" target=\"_blank\" rel=\"noopener\">full video<\/a>, or take a look at <a href=\"http:\/\/spockframework.org\/spock\/docs\/2.0\/all_in_one.html\" target=\"_blank\" rel=\"noopener\">the excellent reference documentation<\/a>.<\/p>\n","protected":false},"author":360,"featured_media":109320,"comment_status":"closed","ping_status":"closed","template":"","categories":[4759,2347,126],"tags":[1130,3178,792,6452,443],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/idea\/109710"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/idea"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/types\/idea"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/users\/360"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/comments?post=109710"}],"version-history":[{"count":9,"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/idea\/109710\/revisions"}],"predecessor-version":[{"id":144479,"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/idea\/109710\/revisions\/144479"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/media\/109320"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/media?parent=109710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/categories?post=109710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/tags?post=109710"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/en\/wp-json\/wp\/v2\/cross-post-tag?post=109710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}