{"id":7525,"date":"2024-01-16T15:53:47","date_gmt":"2024-01-16T14:53:47","guid":{"rendered":"https:\/\/kabisa.nl\/?p=7525"},"modified":"2024-01-17T10:28:05","modified_gmt":"2024-01-17T09:28:05","slug":"do-repeat-yourself-2","status":"publish","type":"post","link":"https:\/\/kabisa.nl\/en\/insights\/embracing-bdd-with-spock-a-worthy-junit-alternative\/","title":{"rendered":"Embracing BDD with Spock, a worthy JUnit alternative?"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"7525\" class=\"elementor elementor-7525\" data-elementor-post-type=\"post\">\n\t\t\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-e74ba16 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"e74ba16\" data-element_type=\"section\" data-e-type=\"section\" data-settings=\"{&quot;jet_parallax_layout_list&quot;:[]}\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-ccec6d4\" data-id=\"ccec6d4\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-6b076b0 elementor-widget elementor-widget-text-editor\" data-id=\"6b076b0\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3>As (Java) developers, testing is a critical part of our workflow, and while JUnit has been a reliable tool,\u00a0<span style=\"text-decoration: underline;\"><a href=\"https:\/\/spockframework.org\/\" target=\"_blank\" rel=\"noopener\">Spock<\/a><\/span>\u00a0is definitely worth a look. With its use of Groovy and focus on BDD (Behavior-Driven Development), Spock enables us to write comprehensive tests that are easy to understand at a glance &#8211; a significant advantage for teams looking to maintain and scale their testing efforts. Let\u2019s take a look at Spock and how it sets itself apart from other testing frameworks like JUnit!<\/h3><div>\u00a0<\/div><h2>What is Spock?\u00a0<\/h2><div><div><div>Spock is a testing and specification framework for Java and Groovy applications that embodies the principles of BDD (Behavior-Driven Development). BDD is an approach to software development that encourages collaboration between developers, QA and non-technical\/business participants in a software project. Spock enables you to write tests that are not only technical representations of the code\u2019s behavior, but also a form of communication.<\/div><\/div><\/div>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-36d7aa2 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"36d7aa2\" data-element_type=\"section\" data-e-type=\"section\" data-settings=\"{&quot;jet_parallax_layout_list&quot;:[]}\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-edbe691\" data-id=\"edbe691\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-d50bb22 elementor-widget elementor-widget-code-highlight\" data-id=\"d50bb22\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard word-wrap\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-java line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-java\">\n\t\t\t\t\t<xmp>def \"Introducing Java developers to Spock through a blogpost might convince them to give it a try\"() {\n  given: \"Someone from Kabisa writes a blogpost\"\n  when: \"Spock's features are highlighted and compared to JUnit\"\n  then: \"Someone reading the blogpost might give Spock a try\"\n}\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-747b217 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"747b217\" data-element_type=\"section\" data-e-type=\"section\" data-settings=\"{&quot;jet_parallax_layout_list&quot;:[]}\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-ac32b96\" data-id=\"ac32b96\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-8aa0aea elementor-widget__width-initial elementor-widget elementor-widget-text-editor\" data-id=\"8aa0aea\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>What makes it stand out is its expressive power and ease of use. It incorporates ideas from other testing frameworks and languages, offering features such as mocking, stubbing and a rich set of matchers. This makes Spock an incredibly powerful tool for writing both simple and complex tests.<\/p>\n<h2>Groovy<\/h2>\n<p>Groovy is a dynamic, object-oriented programming language for the Java platform that offers a more concise and expressive syntax than Java.. It integrates seamlessly with all existing Java code and runs on the JVM. Most importantly, a professional with junior-level experience should have no issues creating tests in Groovy. Groovy\u2019s syntax is easy to understand and more concise than Java, which means your test code is not only shorter but also more readable.<\/p>\n<h2>In practice<\/h2>\n<p>Integrating Spock into your Java project is straightforward. Spock is built with Maven, which means adding it to your project is as simple as including <a href=\"https:\/\/mvnrepository.com\/artifact\/org.spockframework\/spock-core\" target=\"_blank\" rel=\"noopener\">the dependency<\/a> in your pom or build.gradle.<br>While JUnit has been the standard for testing in the Java ecosystem, Spock offers a few enhancements. Let\u2019s take a look at some examples and in some cases compare them with JUnit.<\/p>\n<h3>Expressive and readable<\/h3>\n<p>Spock transforms tests into documentation, clarifying intent and behavior through its structured \u201cgiven-when-then\u201d format. With JUnit the same can be achieved with regular comments, but in my experience these are often neglected.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t<div class=\"elementor-element elementor-element-1ae5f8e e-con-full e-flex e-con e-parent\" data-id=\"1ae5f8e\" data-element_type=\"container\" data-e-type=\"container\" data-settings=\"{&quot;jet_parallax_layout_list&quot;:[]}\">\n\t\t<div class=\"elementor-element elementor-element-2a85496 e-con-full e-flex e-con e-child\" data-id=\"2a85496\" data-element_type=\"container\" data-e-type=\"container\" data-settings=\"{&quot;jet_parallax_layout_list&quot;:[]}\">\n\t\t\t\t<div class=\"elementor-element elementor-element-db9ab03 elementor-widget elementor-widget-text-editor\" data-id=\"db9ab03\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3 style=\"font-family: Poppins, Montserrat, sans-serif; color: #18242b;\"><em>Spock<\/em><\/h3>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-6d29919 elementor-widget elementor-widget-code-highlight\" data-id=\"6d29919\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard word-wrap\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-java line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-java\">\n\t\t\t\t\t<xmp>def \"adding two numbers has the expected outcome\"() {\n    given: \"a calculator\"\n    Calculator calculator = new Calculator()\n\n    when: \"two numbers are added\"\n    def result = calculator.add(2, 3)\n\n    then: \"the result is the sum of the two numbers\"\n    result == 5\n  }\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-d68932d e-con-full e-flex e-con e-child\" data-id=\"d68932d\" data-element_type=\"container\" data-e-type=\"container\" data-settings=\"{&quot;jet_parallax_layout_list&quot;:[]}\">\n\t\t\t\t<div class=\"elementor-element elementor-element-385f9ec elementor-widget elementor-widget-text-editor\" data-id=\"385f9ec\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3 style=\"font-family: Poppins, Montserrat, sans-serif; color: #18242b;\"><i>JUnit<\/i><\/h3>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-6cb1047 elementor-widget elementor-widget-code-highlight\" data-id=\"6cb1047\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard word-wrap\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-java line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-java\">\n\t\t\t\t\t<xmp>    @Test\n    public void addTwoNumbers() {\n        \/\/ a calculator\n        Calculator calculator = new Calculator();\n\n        \/\/ two numbers are added\n        int result = calculator.add(2, 3);\n\n        \/\/ the result is the sum of the two numbers\n        assertEquals(5, result);\n    }\n<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-49b0b67 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"49b0b67\" data-element_type=\"section\" data-e-type=\"section\" data-settings=\"{&quot;jet_parallax_layout_list&quot;:[]}\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-aeb2f12\" data-id=\"aeb2f12\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-10da01e elementor-widget__width-initial elementor-widget elementor-widget-text-editor\" data-id=\"10da01e\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3 style=\"font-family: Poppins, Montserrat, sans-serif; color: #18242b;\"><span style=\"color: var( --e-global-color-text ); font-family: var( --e-global-typography-text-font-family ), Montserrat, sans-serif;\">Test Execution<\/span><\/h3>\n<p style=\"font-family: Poppins, Montserrat, sans-serif; color: #18242b;\"><span style=\"color: var( --e-global-color-text ); font-family: var( --e-global-typography-text-font-family ), Montserrat, sans-serif; font-size: var( --e-global-typography-text-font-size ); font-weight: var( --e-global-typography-text-font-weight );\">Spock enables us to clearly document the way we expect the class we are testing to behave. This in turn provides better maintainability and a better understanding of the test itself and the code we are trying to test. Let\u2019s take a look at test execution inside our IDE (IntelliJ in this case) with some Spock tests.&nbsp;<\/span><\/p>\n<div><img fetchpriority=\"high\" decoding=\"async\" src=\"http:\/\/kabisa.nl\/wp-content\/uploads\/2024\/01\/image1-1.png\" alt=\"\" width=\"765\" height=\"184\"><\/div>\n<div>&nbsp;<\/div>\n<div>Clearly stating what is expected to happen, in normal \u201chuman\u201d language. Now what would the same test execution look like with JUnit?<\/div>\n<p>While JUnit has been the standard for testing in the Java ecosystem, Spock offers a few enhancements. Let\u2019s take a look at some examples and in some cases compare them with JUnit.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/kabisa.nl\/wp-content\/uploads\/2024\/01\/image2-1.png\" alt=\"\" width=\"664\" height=\"188\"><\/p>\n<p><strong>In this case I personally prefer Spock\u2019s expressiveness.<\/strong><\/p>\n<h3>Parameterized tests<\/h3>\n<p>Allows you to write a single test that can cover multiple scenarios. While JUnit also added parameterized tests, Spock&#8217;s approach is more readable and elegant in my opinion.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t<div class=\"elementor-element elementor-element-c90af8b e-con-full e-flex e-con e-parent\" data-id=\"c90af8b\" data-element_type=\"container\" data-e-type=\"container\" data-settings=\"{&quot;jet_parallax_layout_list&quot;:[]}\">\n\t\t<div class=\"elementor-element elementor-element-ab958cd e-con-full e-flex e-con e-child\" data-id=\"ab958cd\" data-element_type=\"container\" data-e-type=\"container\" data-settings=\"{&quot;jet_parallax_layout_list&quot;:[]}\">\n\t\t\t\t<div class=\"elementor-element elementor-element-4751444 elementor-widget elementor-widget-text-editor\" data-id=\"4751444\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3 style=\"font-family: Poppins, Montserrat, sans-serif; color: #18242b;\"><em>Spock<\/em><\/h3>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-45ab095 elementor-widget elementor-widget-code-highlight\" data-id=\"45ab095\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard word-wrap\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-java line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-java\">\n\t\t\t\t\t<xmp>@Unroll   \/* Each row in the table is executed as separate test *\/\n def \"should return #result when #a is added to #b\"() {\n        expect:\n        Math.addExact(a, b) == result\n\n        where:\n        a | b || result\n        1 | 2 || 3\n        5 | 5 || 10\n        2 | 8 || 10\n    }<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-70cb46e e-con-full e-flex e-con e-child\" data-id=\"70cb46e\" data-element_type=\"container\" data-e-type=\"container\" data-settings=\"{&quot;jet_parallax_layout_list&quot;:[]}\">\n\t\t\t\t<div class=\"elementor-element elementor-element-1298464 elementor-widget elementor-widget-text-editor\" data-id=\"1298464\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3 style=\"font-family: Poppins, Montserrat, sans-serif; color: #18242b;\"><em>JUnit<\/em><\/h3>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-f27a012 elementor-widget elementor-widget-code-highlight\" data-id=\"f27a012\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard word-wrap\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-java line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-java\">\n\t\t\t\t\t<xmp>    @ParameterizedTest\n    @CsvSource({\n        \"1, 2, 3\",\n        \"5, 5, 10\",\n        \"2, 8, 10\"\n    })\n    void shouldReturnResultWhenAIsAddedToB(int a, int b, int expectedResult) {\n        Assertions.assertEquals(expectedResult, Math.addExact(a, b));\n    }<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-4e7871f elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"4e7871f\" data-element_type=\"section\" data-e-type=\"section\" data-settings=\"{&quot;jet_parallax_layout_list&quot;:[]}\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-9a9fc41\" data-id=\"9a9fc41\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-aa8c94f elementor-widget__width-initial elementor-widget elementor-widget-text-editor\" data-id=\"aa8c94f\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h3>Mocking and Stubbing<\/h3>\n<p>With Spock, the terms \u201cstub\u201d and \u201cmock\u201d refer to different types of test doubles used to replace the real objects during testing. It does not depend on other libraries such as Mockito for stubbing and mocking.<\/p>\n<ul>\n<li>Stubs provide predefined responses to method calls, they are used to isolate the class you are testing from external dependencies.<\/li>\n<\/ul>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t<div class=\"elementor-element elementor-element-d42e0c4 e-flex e-con-boxed e-con e-parent\" data-id=\"d42e0c4\" data-element_type=\"container\" data-e-type=\"container\" data-settings=\"{&quot;jet_parallax_layout_list&quot;:[]}\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-c4a24e0 elementor-widget elementor-widget-code-highlight\" data-id=\"c4a24e0\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard word-wrap\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-java line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-java\">\n\t\t\t\t\t<xmp>def userRepository = Stub(UserRepository.class);\n\n\/\/ when stub is called with any argument (_), new User is returned.\nuserRepository.findById(_) >> new User(1, \"Kabisa\")<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t<div class=\"elementor-element elementor-element-93ce765 e-flex e-con-boxed e-con e-parent\" data-id=\"93ce765\" data-element_type=\"container\" data-e-type=\"container\" data-settings=\"{&quot;jet_parallax_layout_list&quot;:[]}\">\n\t\t\t\t\t<div class=\"e-con-inner\">\n\t\t\t\t<div class=\"elementor-element elementor-element-fce013a elementor-widget__width-initial elementor-widget elementor-widget-text-editor\" data-id=\"fce013a\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<ul><li>Mocks not only provide predefined responses, but they also record how they were interacted with during the test. With mocks you can verify whether certain methods were called, how often they were called and what arguments they were called with.<\/li><\/ul>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-78aa41b elementor-widget elementor-widget-code-highlight\" data-id=\"78aa41b\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"code-highlight.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<div class=\"prismjs-tomorrow copy-to-clipboard word-wrap\">\n\t\t\t<pre data-line=\"\" class=\"highlight-height language-java line-numbers\">\n\t\t\t\t<code readonly=\"true\" class=\"language-java\">\n\t\t\t\t\t<xmp>def userRepository = Mock(UserRepository.class);\n\n\/\/ Verify's that the userRepository is called once with the id '5'\n1 * userRepository.findById(5);<\/xmp>\n\t\t\t\t<\/code>\n\t\t\t<\/pre>\n\t\t<\/div>\n\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<div class=\"elementor-element elementor-element-309761d elementor-widget elementor-widget-text-editor\" data-id=\"309761d\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<p>Of course these are just a couple comparisons and Spock features. For more you can check out the official documentation\u00a0<a href=\"https:\/\/spockframework.org\/spock\/docs\/2.3\/index.html\" target=\"_blank\" rel=\"noopener\">here<\/a>\u00a0and take a look at Spock\u2019s GitHub\u00a0<a href=\"https:\/\/github.com\/spockframework\/spock\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<section class=\"elementor-section elementor-top-section elementor-element elementor-element-a93bd0a elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"a93bd0a\" data-element_type=\"section\" data-e-type=\"section\" data-settings=\"{&quot;jet_parallax_layout_list&quot;:[]}\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-d8aa222\" data-id=\"d8aa222\" data-element_type=\"column\" data-e-type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t<div class=\"elementor-element elementor-element-6bfb623 elementor-widget__width-initial elementor-widget elementor-widget-text-editor\" data-id=\"6bfb623\" data-element_type=\"widget\" data-e-type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t\t\t<h2><span style=\"color: var( --e-global-color-text );\">Conclusion<\/span><\/h2>\n<p>While JUnit (5!) remains a solid choice, my personal preference leans towards Spock. It&#8217;s a combination of Groovy\u2019s syntax and Spock\u2019s features that help in writing cleaner and more descriptive tests. Although I can understand people being hesitant to use a different language than their application code for testing. I can personally attest that using Groovy for the tests isn\u2019t really an issue, its similarities to Java make the transition almost seamless.<\/p><p>&nbsp; <br><b>All in all,<\/b> JUnit and Spock are both great testing frameworks and it ultimately boils down to personal preference. Hopefully this post showed you that JUnit surely isn\u2019t the only way to write tests for your Java applications. <\/p>\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>As (Java) developers, testing is a critical part of our workflow, and while JUnit has been a reliable tool,\u00a0Spock\u00a0is definitely worth a look. With its use of Groovy and focus on BDD (Behavior-Driven Development), Spock enables us to write comprehensive tests that are easy to understand at a glance &#8211; a significant advantage for teams [&hellip;]<\/p>\n","protected":false},"author":16,"featured_media":7534,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[],"class_list":["post-7525","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tech"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v28.4 (Yoast SEO v28.6) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Embracing BDD with Spock, a worthy JUnit alternative? | Kabisa Software Artisans<\/title>\n<meta name=\"description\" content=\"As (Java) developers, testing is a critical part of our workflow, and while JUnit has been a reliable tool,\u00a0Spock\u00a0is definitely worth a look. With its use\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kabisa.nl\/en\/insights\/embracing-bdd-with-spock-a-worthy-junit-alternative\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Embracing BDD with Spock, a worthy JUnit alternative? | Kabisa Software Artisans\" \/>\n<meta property=\"og:description\" content=\"As (Java) developers, testing is a critical part of our workflow, and while JUnit has been a reliable tool,\u00a0Spock\u00a0is definitely worth a look. With its use\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kabisa.nl\/en\/insights\/embracing-bdd-with-spock-a-worthy-junit-alternative\/\" \/>\n<meta property=\"og:site_name\" content=\"Kabisa Software Artisans\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/kabisaict\" \/>\n<meta property=\"article:published_time\" content=\"2024-01-16T14:53:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-17T09:28:05+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/kabisa.nl\/wp-content\/uploads\/2024\/01\/Embracing-BDD-with-Spock-a-worthy-JUnit-alternative-social-image.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Daan de Rond\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Daan de Rond\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Embracing BDD with Spock, a worthy JUnit alternative? | Kabisa Software Artisans","description":"As (Java) developers, testing is a critical part of our workflow, and while JUnit has been a reliable tool,\u00a0Spock\u00a0is definitely worth a look. With its use","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:\/\/kabisa.nl\/en\/insights\/embracing-bdd-with-spock-a-worthy-junit-alternative\/","og_locale":"en_US","og_type":"article","og_title":"Embracing BDD with Spock, a worthy JUnit alternative? | Kabisa Software Artisans","og_description":"As (Java) developers, testing is a critical part of our workflow, and while JUnit has been a reliable tool,\u00a0Spock\u00a0is definitely worth a look. With its use","og_url":"https:\/\/kabisa.nl\/en\/insights\/embracing-bdd-with-spock-a-worthy-junit-alternative\/","og_site_name":"Kabisa Software Artisans","article_publisher":"https:\/\/www.facebook.com\/kabisaict","article_published_time":"2024-01-16T14:53:47+00:00","article_modified_time":"2024-01-17T09:28:05+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/kabisa.nl\/wp-content\/uploads\/2024\/01\/Embracing-BDD-with-Spock-a-worthy-JUnit-alternative-social-image.png","type":"image\/png"}],"author":"Daan de Rond","twitter_misc":{"Written by":"Daan de Rond","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kabisa.nl\/en\/insights\/embracing-bdd-with-spock-a-worthy-junit-alternative\/#article","isPartOf":{"@id":"https:\/\/kabisa.nl\/en\/insights\/embracing-bdd-with-spock-a-worthy-junit-alternative\/"},"author":{"name":"Daan de Rond","@id":"https:\/\/kabisa.nl\/en\/#\/schema\/person\/1092a65519796d6d607cfba4dc564603"},"headline":"Embracing BDD with Spock, a worthy JUnit alternative?","datePublished":"2024-01-16T14:53:47+00:00","dateModified":"2024-01-17T09:28:05+00:00","mainEntityOfPage":{"@id":"https:\/\/kabisa.nl\/en\/insights\/embracing-bdd-with-spock-a-worthy-junit-alternative\/"},"wordCount":791,"commentCount":0,"publisher":{"@id":"https:\/\/kabisa.nl\/en\/#organization"},"image":{"@id":"https:\/\/kabisa.nl\/en\/insights\/embracing-bdd-with-spock-a-worthy-junit-alternative\/#primaryimage"},"thumbnailUrl":"https:\/\/kabisa.nl\/wp-content\/uploads\/2024\/01\/ce9b1207-109a-4820-8963-b43febf8ee00.jpeg","articleSection":["Tech blog"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/kabisa.nl\/en\/insights\/embracing-bdd-with-spock-a-worthy-junit-alternative\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/kabisa.nl\/en\/insights\/embracing-bdd-with-spock-a-worthy-junit-alternative\/","url":"https:\/\/kabisa.nl\/en\/insights\/embracing-bdd-with-spock-a-worthy-junit-alternative\/","name":"Embracing BDD with Spock, a worthy JUnit alternative? | Kabisa Software Artisans","isPartOf":{"@id":"https:\/\/kabisa.nl\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/kabisa.nl\/en\/insights\/embracing-bdd-with-spock-a-worthy-junit-alternative\/#primaryimage"},"image":{"@id":"https:\/\/kabisa.nl\/en\/insights\/embracing-bdd-with-spock-a-worthy-junit-alternative\/#primaryimage"},"thumbnailUrl":"https:\/\/kabisa.nl\/wp-content\/uploads\/2024\/01\/ce9b1207-109a-4820-8963-b43febf8ee00.jpeg","datePublished":"2024-01-16T14:53:47+00:00","dateModified":"2024-01-17T09:28:05+00:00","description":"As (Java) developers, testing is a critical part of our workflow, and while JUnit has been a reliable tool,\u00a0Spock\u00a0is definitely worth a look. With its use","breadcrumb":{"@id":"https:\/\/kabisa.nl\/en\/insights\/embracing-bdd-with-spock-a-worthy-junit-alternative\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kabisa.nl\/en\/insights\/embracing-bdd-with-spock-a-worthy-junit-alternative\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/kabisa.nl\/en\/insights\/embracing-bdd-with-spock-a-worthy-junit-alternative\/#primaryimage","url":"https:\/\/kabisa.nl\/wp-content\/uploads\/2024\/01\/ce9b1207-109a-4820-8963-b43febf8ee00.jpeg","contentUrl":"https:\/\/kabisa.nl\/wp-content\/uploads\/2024\/01\/ce9b1207-109a-4820-8963-b43febf8ee00.jpeg","width":1024,"height":1024,"caption":"Spock blogpost"},{"@type":"BreadcrumbList","@id":"https:\/\/kabisa.nl\/en\/insights\/embracing-bdd-with-spock-a-worthy-junit-alternative\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kabisa.nl\/en\/"},{"@type":"ListItem","position":2,"name":"Insights","item":"https:\/\/kabisa.nl\/en\/insights\/"},{"@type":"ListItem","position":3,"name":"Embracing BDD with Spock, a worthy JUnit alternative?"}]},{"@type":"WebSite","@id":"https:\/\/kabisa.nl\/en\/#website","url":"https:\/\/kabisa.nl\/en\/","name":"Kabisa Software Artisans","description":"Mission-critical Software for Hightech, Fintech &amp; E-commerce","publisher":{"@id":"https:\/\/kabisa.nl\/en\/#organization"},"alternateName":"Kabisa","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kabisa.nl\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/kabisa.nl\/en\/#organization","name":"Cloud Legends","alternateName":"Kabisa","url":"https:\/\/kabisa.nl\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/kabisa.nl\/en\/#\/schema\/logo\/image\/","url":"https:\/\/kabisa.nl\/wp-content\/uploads\/2022\/12\/kabisa-favicon.png","contentUrl":"https:\/\/kabisa.nl\/wp-content\/uploads\/2022\/12\/kabisa-favicon.png","width":512,"height":512,"caption":"Cloud Legends"},"image":{"@id":"https:\/\/kabisa.nl\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/kabisaict","https:\/\/x.com\/kabisasoftware"],"description":"Software Development is onze core-business. Kabisa ontwikkelt innovatieve maatwerk software die naadloos aansluit bij de behoeftes van klanten en eindgebruikers. Dat doen we in nauwe samenwerking in elke fase van het proces: co-creatie noemen we dat.","email":"info@kabisa.nl","telephone":"0495 430 798","legalName":"Kabisa B.V.","foundingDate":"2006-01-01","numberOfEmployees":{"@type":"QuantitativeValue","minValue":"51","maxValue":"200"}},{"@type":"Person","@id":"https:\/\/kabisa.nl\/en\/#\/schema\/person\/1092a65519796d6d607cfba4dc564603","name":"Daan de Rond","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3ba8a03cef12cd0c8c890f28258923334bc7c767b1faae73ca70e492da46a7ed?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3ba8a03cef12cd0c8c890f28258923334bc7c767b1faae73ca70e492da46a7ed?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3ba8a03cef12cd0c8c890f28258923334bc7c767b1faae73ca70e492da46a7ed?s=96&d=mm&r=g","caption":"Daan de Rond"}}]}},"_links":{"self":[{"href":"https:\/\/kabisa.nl\/en\/wp-json\/wp\/v2\/posts\/7525","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kabisa.nl\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kabisa.nl\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kabisa.nl\/en\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/kabisa.nl\/en\/wp-json\/wp\/v2\/comments?post=7525"}],"version-history":[{"count":19,"href":"https:\/\/kabisa.nl\/en\/wp-json\/wp\/v2\/posts\/7525\/revisions"}],"predecessor-version":[{"id":7555,"href":"https:\/\/kabisa.nl\/en\/wp-json\/wp\/v2\/posts\/7525\/revisions\/7555"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kabisa.nl\/en\/wp-json\/wp\/v2\/media\/7534"}],"wp:attachment":[{"href":"https:\/\/kabisa.nl\/en\/wp-json\/wp\/v2\/media?parent=7525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kabisa.nl\/en\/wp-json\/wp\/v2\/categories?post=7525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kabisa.nl\/en\/wp-json\/wp\/v2\/tags?post=7525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}