<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" 
  xmlns:content="http://purl.org/rss/1.0/modules/content/" 
  xmlns:dc="http://purl.org/dc/elements/1.1/" 
  xmlns:atom="http://www.w3.org/2005/Atom" 
  xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" 
  xmlns:media="http://search.yahoo.com/mrss/">
  <channel>
    <title>spring-boot on Solid Soft</title>
    <link>https://blog.solidsoft.pl/tags/spring-boot/</link>
    <description>Recent content in spring-boot on Solid Soft</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <copyright>©{year}, All Rights Reserved</copyright>
    <lastBuildDate>Fri, 14 Sep 2018 10:00:00 +0200</lastBuildDate>
    <sy:updatePeriod>hourly</sy:updatePeriod>
    <sy:updateFrequency>2</sy:updateFrequency>
    
        <atom:link href="https://blog.solidsoft.pl/tags/spring-boot/index.xml" rel="self" type="application/rss+xml" />
    
    
    

      
      <item>
        <title>Simplify integration testing of legacy applications with Spock 1.2</title>
        <link>https://blog.solidsoft.pl/2018/09/14/simplify-integration-testing-of-legacy-applications-with-spock-1.2/</link>
        <pubDate>Fri, 14 Sep 2018 10:00:00 +0200</pubDate>
        
        <atom:modified>Fri, 14 Sep 2018 10:00:00 +0200</atom:modified>
        <guid>https://blog.solidsoft.pl/2018/09/14/simplify-integration-testing-of-legacy-applications-with-spock-1.2/</guid>
        <description>Learn how leverage Spock 1.2 to slice a Spring context of a legacy application writing integration tests.
 Have you ever wanted, having some legacy application which you were starting to work on, to write some tests to get know what is going on and possibly be notified about regressions? That feeling when you want to instantiate a single class and it fails with NullPointerException. 6 replaced (with difficulty) dependencies later there are still some errors from the classes that you haven&amp;rsquo;t heard about before.</description>
        <content:encoded>&lt;blockquote&gt;
&lt;p&gt;Learn how leverage Spock 1.2 to slice a Spring context of a legacy application writing integration tests.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Have you ever wanted, having some legacy application which you were starting to work on, to write some tests to get know what is going on and possibly be notified about regressions? That feeling when you want to instantiate a single class and it fails with &lt;code&gt;NullPointerException&lt;/code&gt;. 6 replaced (with difficulty) dependencies later there are still some errors from the classes that you haven&amp;rsquo;t heard about before. Sounds familiar?&lt;/p&gt;
&lt;p&gt;There are different techniques to deal with hidden dependencies. There is the whole dedicated &lt;a href=&#34;https://www.amazon.com/Working-Effectively-Legacy-Michael-Feathers/dp/0131177052&#34; target=&#34;_blank&#34;&gt;book&lt;/a&gt;
 about that (and probably a few other that I haven&amp;rsquo;t read). Occasionally, it may be feasible to start with the integration tests and run through some process. It may be even more &amp;ldquo;entertaining&amp;rdquo; to see what exotic components are required to just setup the context, even if they are completely not needed in our case. Thank you (too wide and carelessly used) &lt;code&gt;@ComponentScan&lt;/code&gt; :).&lt;/p&gt;
&lt;p&gt;Injecting stubs/mocks inside the test context is a way to go as an emergency assistance (see the last paragraph, there are better, yet harder approaches). It can be achieved &amp;ldquo;manually&amp;rdquo; with an extra bean definition with the &lt;code&gt;@Primary&lt;/code&gt; annotation (usually a reason to think twice before doing that) for every dependency at which level we want to make a cut of (or for every unneeded bean which is instantiated by the way). &lt;code&gt;@MockBean&lt;/code&gt; placed on a field in a test is more handy, but still, it is needed to define a field in our tests and put the annotation on it (5? 10? 15 beans?). Spock 1.2 introduces somehow less know feature which may be useful here - &lt;code&gt;@StubBeans&lt;/code&gt;.&lt;/p&gt;




  



  &lt;div class=&#34;featured-image__wrapper&#34;&gt;
    &lt;img src=&#34;https://blog.solidsoft.pl/images/posts/2018/mocked-dependencies-spring.png&#34; alt=&#34;Featured Image&#34; class=&#34;featured-image&#34; style=&#34;width: 680px;height: 356px;&#34;&gt;
  &lt;/div&gt;

&lt;p&gt;It can be used to simply provide a list of classes which (possible) instances should be replaced with stubs in the Spring test context. Of course before the real objects are being instantiated (to prevent for example NPE in a constructor). Thanks to that up to several lines of stubbing/mock injections:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;27
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;28
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;29
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;30
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-groovy&#34; data-lang=&#34;groovy&#34;&gt;&lt;span class=&#34;nd&#34;&gt;@RunWith&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SpringRunner&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;na&#34;&gt;class&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;//Spring Boot + Mockito
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;&lt;span class=&#34;nd&#34;&gt;@SpringBootTest&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;//possibly some Spring configuration with @ComponentScan is imported in this legacy application
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;BasicPathReportGeneratorInLegacyApplicationITTest&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;{&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;//usual approach
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;
    &lt;span class=&#34;nd&#34;&gt;@MockBean&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;KafkaClient&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;kafkaClientMock&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;;&lt;/span&gt;

    &lt;span class=&#34;nd&#34;&gt;@MockBean&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;FancySelfieEnhancer&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fancySelfieEnhancerMock&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;;&lt;/span&gt;

    &lt;span class=&#34;nd&#34;&gt;@MockBean&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;FastTwitterSubscriber&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;fastTwitterSubscriberMock&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;;&lt;/span&gt;

    &lt;span class=&#34;nd&#34;&gt;@MockBean&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;WaterCoolerWaterLevelAterter&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;waterCoolerWaterLevelAterterMock&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;;&lt;/span&gt;

    &lt;span class=&#34;nd&#34;&gt;@MockBean&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NsaSilentNotifier&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;nsaSilentNotifierMock&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;;&lt;/span&gt;

    &lt;span class=&#34;c1&#34;&gt;//a few more - remember, this is legacy application, genuine since 1999 ;)
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;    &lt;span class=&#34;c1&#34;&gt;//...
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;
    &lt;span class=&#34;nd&#34;&gt;@Autowired&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReportGenerator&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;reportGenerator&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;;&lt;/span&gt;

    &lt;span class=&#34;nd&#34;&gt;@Test&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;public&lt;/span&gt; &lt;span class=&#34;kt&#34;&gt;void&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;shouldGenerateEmptyReportForEmptyInputData&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;{&lt;/span&gt;
        &lt;span class=&#34;o&#34;&gt;...&lt;/span&gt;
    &lt;span class=&#34;o&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;o&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;can be replaced with just one (long) line:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-groovy&#34; data-lang=&#34;groovy&#34;&gt;&lt;span class=&#34;nd&#34;&gt;@SpringBootTest&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;//possibly some Spring configuration with @ComponentScan is imported
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;&lt;span class=&#34;nd&#34;&gt;@StubBeans&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;([&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;KafkaClient&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;FancySelfieEnhancer&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;FastTwitterSubscriber&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;WaterCoolerWaterLevelAterter&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;NsaSilentNotifier&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;/(, ... */&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;])&lt;/span&gt;
  &lt;span class=&#34;c1&#34;&gt;//all classes of real beans which should be replaced with stubs
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;BasicPathReportGeneratorInLegacyApplicationITSpec&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;extends&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Specification&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;{&lt;/span&gt;

    &lt;span class=&#34;nd&#34;&gt;@Autowired&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ReportGenerator&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;reportGenerator&lt;/span&gt;

    &lt;span class=&#34;kt&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;should generate empty report for empty input data&amp;#34;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;{&lt;/span&gt;
        &lt;span class=&#34;o&#34;&gt;....&lt;/span&gt;
    &lt;span class=&#34;o&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;o&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;(tested with Spock 1.2-RC2)&lt;/p&gt;
&lt;p&gt;It&amp;rsquo;s worth to mention that &lt;code&gt;@StubBeans&lt;/code&gt; is intended just to provide placeholders. In a situation it is required to provide stubbing and/or an invocation verification &lt;code&gt;@SpringBean&lt;/code&gt; or &lt;code&gt;@SpringSpy&lt;/code&gt; (also introduced in Spock 1.2) are better. I wrote more about it in my previous &lt;a href=&#34;https://solidsoft.wordpress.com/2018/09/03/spock-1-2-hassle-free-spring-beans-mocking-in-integration-tests/&#34; target=&#34;_blank&#34;&gt;blog post&lt;/a&gt;
.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;There is one important aspect to emphasize&lt;/strong&gt;. &lt;code&gt;@StubBeans&lt;/code&gt; are handy to be used in a situation when we have some &amp;ldquo;legacy&amp;rdquo; project and want to start writing integration regression tests quickly to see the results. However, as a colleague of mine &lt;a href=&#34;https://stackoverflow.com/users/719633/roadrunner&#34; target=&#34;_blank&#34;&gt;Darek Kaczyński&lt;/a&gt;
 brightly summarized, blindly replacing beans which &amp;ldquo;explode&amp;rdquo; in tests is just &amp;ldquo;sweeping problems under carpet&amp;rdquo;. After the initial phase, when we are starting to understand what is going on, it is a good moment to rethink the way the context - both in production and in tests - is created. The already mentioned too wide &lt;code&gt;@ComponentScan&lt;/code&gt; is very often the root of all evil. An ability to setup a partial context and put it together (if needed) is a good place to start. Using &lt;code&gt;@Profile&lt;/code&gt; or conditional beans are the very powerful mechanisms in tests (and not only there). &lt;code&gt;@TestConfiguration&lt;/code&gt; and proper bean selection to improve context caching are something worth to keep in your mind. However, I started this article to present the new mechanism in Spock which might be useful in some cases and I want to keep it short. There could be an another, more generic blog post just about managing the Spring context in the integration tests. I have to seriously thing about it :).&lt;/p&gt;
</content:encoded>
        <dc:creator>Marcin Zajączkowski</dc:creator>
        <media:content url="https://blog.solidsoft.pl/images/posts/2018/mocked-dependencies-spring.png" medium="image"><media:title type="html">featured image</media:title></media:content>
        
        
        
          
            
              <category>groovy</category>
            
          
            
              <category>integration-testing</category>
            
          
            
              <category>mocking</category>
            
          
            
              <category>mockito</category>
            
          
            
              <category>spock</category>
            
          
            
              <category>spock-1.2</category>
            
          
            
              <category>spring</category>
            
          
            
              <category>spring-boot</category>
            
          
            
              <category>testing</category>
            
          
            
              <category>tests</category>
            
          
        
        
          
            
              <category>Tools</category>
            
          
            
              <category>Tricks &amp; Tips</category>
            
          
        
        
      </item>
      
      <item>
        <title>Spock 1.2 - hassle-free Spring beans mocking in integration tests</title>
        <link>https://blog.solidsoft.pl/2018/09/03/spock-1.2-hassle-free-spring-beans-mocking-in-integration-tests/</link>
        <pubDate>Mon, 03 Sep 2018 10:00:00 +0200</pubDate>
        
        <atom:modified>Mon, 03 Sep 2018 10:00:00 +0200</atom:modified>
        <guid>https://blog.solidsoft.pl/2018/09/03/spock-1.2-hassle-free-spring-beans-mocking-in-integration-tests/</guid>
        <description>Discover how to automatically inject Spock&amp;rsquo;s mocks and spies into the Spring context using Spock 1.2.
  Stubs/mocks/spies in Spock (and their life cycle) have been always tightly coupled with the Spock Specification class. It was only possible to create them in a test class. Therefore, using shared, predefined mocks (in both unit and integration tests) was problematic.
The situation was slightly improved in Spock 1.1, but only with the brand new Spock 1.</description>
        <content:encoded>&lt;blockquote&gt;
&lt;p&gt;Discover how to automatically inject Spock&amp;rsquo;s mocks and spies into the Spring context using Spock 1.2.&lt;/p&gt;
&lt;/blockquote&gt;


&lt;figure &gt;
  
    &lt;img src=&#34;https://blog.solidsoft.pl/images/posts/2018/spock12-spring-bean-mocking960-ratio.jpg&#34; alt=&#34;A lot of coffee beans with a partially filled cup laying on it. 3 beans in a cup have different color&#34; style=&#34;width:;height:;&#34;/&gt;
  
  
&lt;/figure&gt;

&lt;br&gt;
&lt;p&gt;Stubs/mocks/spies in Spock (and their life cycle) have been always tightly coupled with the Spock &lt;code&gt;Specification&lt;/code&gt; class. It was only possible to create them in a test class. Therefore, using shared, predefined mocks (in both unit and integration tests) was problematic.&lt;/p&gt;
&lt;p&gt;The situation was slightly improved in Spock 1.1, but only with the brand new Spock 1.2 (1.2-RC1 as a time of writing) using the Spock mocking subsystem in Spring-based integration tests is as easy as using &lt;code&gt;@SpringMock&lt;/code&gt; for Mockito mocks in Spring Boot. Let&amp;rsquo;s check it up.&lt;/p&gt;
&lt;p&gt;Btw, to be more cutting edge in addition to Spock 1.2-RC1, I will be using Spring Boot 2.1.0.M2, Spring 5.1.0.RC2 and Groovy 2.5.2 (but everything should work with the stable versions of Spring (Boot) and Groovy 2.4).&lt;/p&gt;
&lt;p&gt;One more thing. For the sake of simplicity, in this article, I will be using a term &amp;lsquo;mock&amp;rsquo; to refer also stubs and spies. They &lt;a href=&#34;https://martinfowler.com/articles/mocksArentStubs.html#TheDifferenceBetweenMocksAndStubs&#34; target=&#34;_blank&#34;&gt;differs&lt;/a&gt;
 in behavior, however, in a scope of injecting it into the Spring context in the Spock tests it usually doesn&amp;rsquo;t matter.&lt;/p&gt;
&lt;h2 id=&#34;spock-11---manual-way&#34;&gt;Spock 1.1 - manual way&lt;/h2&gt;
&lt;p&gt;Thanks to the work of &lt;a href=&#34;https://github.com/leonard84&#34; target=&#34;_blank&#34;&gt;Leonard Brünings&lt;/a&gt;
, mocks in Spock were decoupled from the &lt;code&gt;Specification&lt;/code&gt; class. It was finally possible to create them outside and to attach it later on into a running test. It was the cornerstone of using Spock mocks in the Spring (or any other) context.&lt;/p&gt;
&lt;p&gt;In this sample code we have the &lt;code&gt;ShipDatabase&lt;/code&gt; class which uses &lt;code&gt;OwnShipIndex&lt;/code&gt; and &lt;code&gt;EnemyShipIndex&lt;/code&gt; (of course injected by a constructor :) ) to return aggregated information about all known ships matched by name.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;27
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;28
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;29
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;30
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;31
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;32
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;33
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;34
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;35
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;36
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;37
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;38
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;39
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;40
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;41
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;42
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;43
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;44
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-groovy&#34; data-lang=&#34;groovy&#34;&gt;&lt;span class=&#34;c1&#34;&gt;//@ContextConfiguration just for simplification, @(Test)Configuration is usually more convenient for Spring Boot tests
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;//Real beans can exist in the context or not
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;&lt;span class=&#34;nd&#34;&gt;@ContextConfiguration&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;classes&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ShipDatabase&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;TestConfig&lt;/span&gt;&lt;span class=&#34;cm&#34;&gt;/*, OwnShipIndex, EnemyShipIndex*/&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;])&lt;/span&gt;
&lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ShipDatabase11ITSpec&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;extends&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Specification&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;{&lt;/span&gt;

    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;static&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;final&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;String&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ENTERPRISE_D&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;USS Enterprise (NCC-1701-D)&amp;#34;&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;static&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;final&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;String&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;BORTAS_ENTERA&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;IKS Bortas Entera&amp;#34;&lt;/span&gt;

    &lt;span class=&#34;nd&#34;&gt;@Autowired&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;OwnShipIndex&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ownShipIndexMock&lt;/span&gt;

    &lt;span class=&#34;nd&#34;&gt;@Autowired&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EnemyShipIndex&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;enemyShipIndexMock&lt;/span&gt;

    &lt;span class=&#34;nd&#34;&gt;@Autowired&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ShipDatabase&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;shipDatabase&lt;/span&gt;

    &lt;span class=&#34;kt&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;should find ship in both indexes&amp;#34;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;{&lt;/span&gt;
        &lt;span class=&#34;nl&#34;&gt;given:&lt;/span&gt;
            &lt;span class=&#34;n&#34;&gt;ownShipIndexMock&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;na&#34;&gt;findByName&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;Enter&amp;#34;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ENTERPRISE_D&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;
            &lt;span class=&#34;n&#34;&gt;enemyShipIndexMock&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;na&#34;&gt;findByName&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;Enter&amp;#34;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;BORTAS_ENTERA&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;
        &lt;span class=&#34;nl&#34;&gt;when:&lt;/span&gt;
            &lt;span class=&#34;n&#34;&gt;List&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;String&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;foundShips&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;shipDatabase&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;na&#34;&gt;findByName&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;Enter&amp;#34;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt;
        &lt;span class=&#34;nl&#34;&gt;then:&lt;/span&gt;
            &lt;span class=&#34;n&#34;&gt;foundShips&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ENTERPRISE_D&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;BORTAS_ENTERA&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;
    &lt;span class=&#34;o&#34;&gt;}&lt;/span&gt;

&lt;span class=&#34;c1&#34;&gt;//    @TestConfiguration
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;    &lt;span class=&#34;kd&#34;&gt;static&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;TestConfig&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;{&lt;/span&gt;
        &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DetachedMockFactory&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;detachedMockFactory&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;new&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;DetachedMockFactory&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;()&lt;/span&gt;

        &lt;span class=&#34;nd&#34;&gt;@Bean&lt;/span&gt;
        &lt;span class=&#34;nd&#34;&gt;@Primary&lt;/span&gt;    &lt;span class=&#34;c1&#34;&gt;//if needed, beware of consequences
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;        &lt;span class=&#34;n&#34;&gt;OwnShipIndex&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;ownShipIndexStub&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;{&lt;/span&gt;
            &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;detachedMockFactory&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;na&#34;&gt;Stub&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;OwnShipIndex&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt;
        &lt;span class=&#34;o&#34;&gt;}&lt;/span&gt;

        &lt;span class=&#34;nd&#34;&gt;@Bean&lt;/span&gt;
        &lt;span class=&#34;nd&#34;&gt;@Primary&lt;/span&gt;    &lt;span class=&#34;c1&#34;&gt;//if needed, beware of consequences
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;        &lt;span class=&#34;n&#34;&gt;EnemyShipIndex&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;enemyShipIndexStub&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;{&lt;/span&gt;
            &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;detachedMockFactory&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;na&#34;&gt;Stub&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;EnemyShipIndex&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt;
        &lt;span class=&#34;o&#34;&gt;}&lt;/span&gt;
    &lt;span class=&#34;o&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;o&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;The mocks are created in a separate class (outside the &lt;code&gt;Specification&lt;/code&gt;) and therefore &lt;code&gt;DetachedMockFactory&lt;/code&gt; has to be used (or alternatively &lt;code&gt;SpockMockFactoryBean&lt;/code&gt;). Those mocks have to be attached (and detached) to the test instance (the &lt;code&gt;Specification&lt;/code&gt; instance), but it is automatically handled by the &lt;code&gt;spock-spring&lt;/code&gt; module (as of 1.1). For generic mocks created externally also &lt;code&gt;MockUtil.attachMock()&lt;/code&gt; and &lt;code&gt;mockUtil.detachMock()&lt;/code&gt; would need to be used to make it work.&lt;/p&gt;
&lt;p&gt;As a result it was possible to create and use mocks in the Spring context, but it was not very convenient and it was not commonly used.&lt;/p&gt;
&lt;h2 id=&#34;spock-12---first-class-support&#34;&gt;Spock 1.2 - first class support&lt;/h2&gt;
&lt;p&gt;Spring Boot 1.4 brought the new quality to integration testing with (Mockito&amp;rsquo;s) mocks. It leveraged the idea, originally presented in &lt;a href=&#34;https://github.com/springockito/springockito/&#34; target=&#34;_blank&#34;&gt;Springockito&lt;/a&gt;
 back in 2012 (when the Spring configuration was mostly written in XML :) ) to automatically inject mocks (or spies) into the Spring (Boot) context. The Spring Boot team extended the idea and thanks to having it as the internally supported feature it (usually) works reliably just by adding an annotation or two in your test.&lt;/p&gt;
&lt;p&gt;Similar annotation-based mechanism is built-in in Spock &lt;a href=&#34;https://search.maven.org/search?q=g:org.spockframework%20AND%20a:spock-core&amp;amp;core=gav&#34; target=&#34;_blank&#34;&gt;1.2&lt;/a&gt;
.&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt; 1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 7
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 8
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt; 9
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;10
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;11
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;12
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;13
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;14
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;15
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;16
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;17
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;18
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;19
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;20
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;21
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;22
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;23
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;24
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;25
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;26
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;27
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-groovy&#34; data-lang=&#34;groovy&#34;&gt;&lt;span class=&#34;c1&#34;&gt;//@ContextConfiguration just for simplification, @(Test)Configuration is usually more convenient for Spring Boot tests
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;//Real beans can exist in the context or not
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;&lt;span class=&#34;nd&#34;&gt;@ContextConfiguration&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;classes&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ShipDatabase&lt;/span&gt;&lt;span class=&#34;cm&#34;&gt;/*, OwnShipIndex, EnemyShipIndex*/&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;])&lt;/span&gt;
&lt;span class=&#34;kd&#34;&gt;class&lt;/span&gt; &lt;span class=&#34;nc&#34;&gt;ShipDatabaseITSpec&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;extends&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Specification&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;{&lt;/span&gt;

    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;static&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;final&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;String&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ENTERPRISE_D&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;USS Enterprise (NCC-1701-D)&amp;#34;&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;static&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;final&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;String&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;BORTAS_ENTERA&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;IKS Bortas Entera&amp;#34;&lt;/span&gt;

    &lt;span class=&#34;nd&#34;&gt;@SpringBean&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;OwnShipIndex&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ownShipIndexMock&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Stub&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;()&lt;/span&gt;  &lt;span class=&#34;c1&#34;&gt;//could be Mock() if needed
&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;&lt;/span&gt;
    &lt;span class=&#34;nd&#34;&gt;@SpringBean&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;EnemyShipIndex&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;enemyShipIndexMock&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Stub&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;()&lt;/span&gt;

    &lt;span class=&#34;nd&#34;&gt;@Autowired&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;private&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ShipDatabase&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;shipDatabase&lt;/span&gt;

    &lt;span class=&#34;kt&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;#34;should find ship in both indexes&amp;#34;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;{&lt;/span&gt;
        &lt;span class=&#34;nl&#34;&gt;given:&lt;/span&gt;
            &lt;span class=&#34;n&#34;&gt;ownShipIndexMock&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;na&#34;&gt;findByName&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;Enter&amp;#34;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ENTERPRISE_D&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;
            &lt;span class=&#34;n&#34;&gt;enemyShipIndexMock&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;na&#34;&gt;findByName&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;Enter&amp;#34;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;BORTAS_ENTERA&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;
        &lt;span class=&#34;nl&#34;&gt;when:&lt;/span&gt;
            &lt;span class=&#34;n&#34;&gt;List&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;String&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;foundShips&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;shipDatabase&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;na&#34;&gt;findByName&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;#34;Enter&amp;#34;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;)&lt;/span&gt;
        &lt;span class=&#34;nl&#34;&gt;then:&lt;/span&gt;
            &lt;span class=&#34;n&#34;&gt;foundShips&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;==&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;ENTERPRISE_D&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;BORTAS_ENTERA&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;]&lt;/span&gt;
    &lt;span class=&#34;o&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;o&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;There is not much to be added. &lt;code&gt;@SpringBean&lt;/code&gt; instructs Spock to inject a mock into a Spring context. Similarly, &lt;code&gt;@SpringSpy&lt;/code&gt; wraps the real bean with a spy. In a case of &lt;code&gt;@SpringBean&lt;/code&gt; it is required to initialize a field to let Spock know if we plan to use a stub or a mock.&lt;/p&gt;
&lt;p&gt;In addition, there is also a more general annotation &lt;code&gt;@StubBeans&lt;/code&gt; to replace all defined beans with stubs. However, I plan to cover it separately in an another blog post.&lt;/p&gt;
&lt;h2 id=&#34;limitations&#34;&gt;Limitations&lt;/h2&gt;
&lt;p&gt;For those of you who look forward to rewrite all Mockito&amp;rsquo;s mocks to Spock&amp;rsquo;s mocks in your Spock tests right after the lecture of this article there is a word of warning. Spock&amp;rsquo;s mocks - due to their nature and relation to &lt;code&gt;Specification&lt;/code&gt; - have some limitations. The implementation under the hood creates a proxy which is injected into the Spring context which (potentially) replaces real beans (stubs/mocks) or wraps them (spies). That proxy is shared between all the tests in the particular test (specification) class. In fact, it also can span across other tests with the same bean/mock declarations in the situation Spring is able to cache the context (similar situation to Mockito&amp;rsquo;s mocks or Spring integration tests in general).&lt;/p&gt;
&lt;p&gt;However, what is really important, a proxy is attached to a tests right before its execution and is detached right after it. Therefore, in fact, every test has it&amp;rsquo;s own mock instance (it cannot be applied to &lt;code&gt;@Shared&lt;/code&gt; fields) and it is problematic for instance to group interactions from different tests and verify them together (which usually is quite sensible, but might lead to some duplication). Nevertheless, with using a &lt;code&gt;setup&lt;/code&gt; block (or in-line stubbing) it is possible to share stubbing and interaction expectancy.&lt;/p&gt;
&lt;h2 id=&#34;summary&#34;&gt;Summary&lt;/h2&gt;
&lt;p&gt;Spock 1.2 finally brings hassle-free Spock&amp;rsquo;s stubs/mocks/spies support for using them in the Spring context which is comparable with the one provided in Spring Boot for Mockito. It is just enough to add the &lt;code&gt;spock-spring&lt;/code&gt; module to the project (runtime) dependencies. Despite some limitations, it is one point less for mixing native Spock&amp;rsquo;s mocking subsystem with external mocking frameworks (such as Mockito) in your Spock (integration) tests. And what is nice, it should work also in plain Spring Framework tests (not only Spring Boot tests). The same feature has been implemented for Guice (but I haven&amp;rsquo;t tested it).&lt;/p&gt;
&lt;p&gt;Furthermore, Spock 1.2 brings also some other &lt;a href=&#34;https://github.com/spockframework/spock/blob/master/docs/release_notes.adoc&#34; target=&#34;_blank&#34;&gt;changes&lt;/a&gt;
 including better support for Java 9+ and it is worth to give it a try in your test suite (and of course &lt;a href=&#34;https://github.com/spockframework/spock/issues&#34; target=&#34;_blank&#34;&gt;report&lt;/a&gt;
 any potentially spotted regression bugs :) ).&lt;/p&gt;
&lt;p&gt;One more good news. In addition to the Leonard&amp;rsquo;s work who made Spock 1.2 possible and a legion of bug reporters and PR contributors, since recently, there are also some other committers who are working on making Spock even better. Some of them you may know from some other popular FOSS projects. What is more, Spock 1.2 is &lt;em&gt;(preliminary) planned&lt;/em&gt; to be the last version based on JUnit 4 and the next stable Spock version could be 2.0, leveraging JUnit 5 and (among others) its native ability to run tests in parallel.&lt;/p&gt;
&lt;p&gt;The examples were written using Spock 1.2-RC1. It will be updated to 1.2-final once released. The source code is available from GitHub.&lt;/p&gt;
&lt;p&gt;Btw, have you wonder if it is still worth using Spock in the time of JUnit 5? I try to help answer that question in my presentation which will be possible to &lt;a href=&#34;https://jdd.org.pl/lecture.html#id=47734&#34; target=&#34;_blank&#34;&gt;see&lt;/a&gt;
 at &lt;a href=&#34;https://jdd.org.pl/&#34; target=&#34;_blank&#34;&gt;JDD 2018&lt;/a&gt;
, this October in Kraków, Poland. See you there.&lt;/p&gt;
&lt;p&gt;&lt;a href=&#34;https://jdd.org.pl/lecture.html#id=47734&#34; target=&#34;_blank&#34;&gt;

&lt;figure &gt;
  
    &lt;img data-src=&#34;https://blog.solidsoft.pl/images/posts/2018/jdd2018-logo-with-date.png&#34; alt=&#34;JDD 2018 logo with date&#34; data-caption=&#34;&#34; src=&#34;data:image/svg+xml,%0A%3Csvg xmlns=&#39;http://www.w3.org/2000/svg&#39; width=&#39;614px&#39; height=&#39;204&#39; viewBox=&#39;0 0 24 24&#39;%3E%3Cpath fill=&#39;none&#39; d=&#39;M0 0h24v24H0V0z&#39;/%3E%3Cpath fill=&#39;%23aaa&#39; d=&#39;M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm-1 16H6c-.55 0-1-.45-1-1V6c0-.55.45-1 1-1h12c.55 0 1 .45 1 1v12c0 .55-.45 1-1 1zm-4.44-6.19l-2.35 3.02-1.56-1.88c-.2-.25-.58-.24-.78.01l-1.74 2.23c-.26.33-.02.81.39.81h8.98c.41 0 .65-.47.4-.8l-2.55-3.39c-.19-.26-.59-.26-.79 0z&#39;/%3E%3C/svg%3E&#34; class=&#34;lazyload&#34; style=&#34;width:614px;height:204;&#34;/&gt;
    
  
&lt;/figure&gt;
&lt;/a&gt;
&lt;/p&gt;
&lt;span style=&#34;opacity: 0.6&#34;&gt;The lead photo based on the &lt;a href=&#34;https://pixabay.com/en/users/Couleur-1195798/&#34;&gt;Couleur&lt;/a&gt;&amp;lsquo;s &lt;a href=&#34;https://pixabay.com/en/coffee-beans-coffee-cup-cup-coffee-3392159/&#34;&gt;work&lt;/a&gt; published in &lt;a href=&#34;https://pixabay.com/&#34;&gt;Pixabay&lt;/a&gt;, &lt;a href=&#34;https://creativecommons.org/publicdomain/zero/1.0/deed.en&#34;&gt;CC0 1.0&lt;/a&gt;&lt;/span&gt;
</content:encoded>
        <dc:creator>Marcin Zajączkowski</dc:creator>
        <media:content url="https://blog.solidsoft.pl/images/posts/2018/spock12-spring-bean-mocking960.jpg" medium="image"><media:title type="html">featured image</media:title></media:content>
        
        
        
          
            
              <category>mocking</category>
            
          
            
              <category>spock</category>
            
          
            
              <category>spock-1.2</category>
            
          
            
              <category>spring</category>
            
          
            
              <category>spring-boot</category>
            
          
            
              <category>testing</category>
            
          
            
              <category>tests</category>
            
          
        
        
          
            
              <category>Tools</category>
            
          
        
        
      </item>
      

    
  </channel>
</rss>