title: How to stub Spring repository to throw an exception in an integration test?
body: <p>I have a service which has a repository as a constructor's parameter.</p>
  
  <pre><code>@Autowired
  NodeServiceListInstallService( final BykiListRepository aBykiListRepository )
  </code></pre>
  
  <p>The <code>BykiListRepository</code> is default Spring repository without implementation</p>
  
  <pre><code>interface BykiListRepository extends JpaRepository&lt;BykiList, Long&gt; {
      //some magic methods
  }
  </code></pre>
  
  <p>My configuration class marked with <code>@EnableJpaRepositories</code>, so, I haven't direct bean's declaration. Service declaration:</p>
  
  <pre><code>@SpringBootApplication
  @EnableConfigurationProperties( ApplicationProperties )
  @EnableJpaRepositories
  @EnableTransactionManagement
  @ImportResource( 'classpath:META-INF/spring/application-context.xml' )
  class Application extends SpringBootServletInitializer {
      @Bean
      NodeServiceListInstallService nodeServiceListInstallService( final BykiListRepository bykiListRepository ) {
          new NodeServiceListInstallService( bykiListRepository )
      }
  }
  </code></pre>
  
  <p>I'm trying to write a test within of the call of the repository's method <code>save</code> will thrown an exception <code>PersistenceException</code>.
  I've tried to stub/spy a a repository and declare it as a bean in <code>@TestConfiguration</code> with <code>@Primary</code>, or even implement the interface.
  But I haven't got the result.</p>
  
  <pre><code>@TestConfiguration
  class TestConfig {
      @Bean
      BykiListRepository bykiListRepository() {
          //return Spock's Spy/Stub or new BykiRepositoryBadImpl()
      }
  </code></pre>
  
  <p>Test:</p>
  
  <pre><code>@ContextConfiguration( classes = TestConfig )
  class GlobalErrorHandlerIntegrationTest extends BaseFlowIntegrationTest {
      //test()
  }
  </code></pre>
  
  <p>I write on <code>Groovy-2.4.12</code> and write tests with <code>Spock-1.1</code>. <code>Spring Boot 1.5.4</code>.
  Reserved variant is to use aspect, but there's not exactly what I wish.
  Will be very grateful for help.</p>
  
  <p>Update: Usage of <code>DetachedMockFactory</code>:</p>
  
  <p>Configuration:</p>
  
  <p>@TestConfiguration</p>
  
  <pre><code>class DummyConfiguration {
      private final detachedFactory = new DetachedMockFactory()
      @Bean
      @Primary
      BykiListRepository bykiListRepository() {
          detachedFactory.Mock( BykiListRepository )
      }
  }
  </code></pre>
  
  <p>Test's skeleton:</p>
  
  <pre><code>@SpringBootTest( classes = DummyConfiguration )
  @Import( [DummyConfiguration] )
  @ContextConfiguration( classes = DummyConfiguration )
  class GlobalErrorHandlerIntegrationTest extends BaseFlowIntegrationTest {
      @Autowired
      BykiListRepository bykiListRepositoryMock
      def 'exercise error handling'() {
          given: 'the failing repository'
          bykiListRepositoryMock.save( _ ) &gt;&gt; {
              throw new CannotCreateTransactionException()
          }
          when: 'the message is send to rabbit'
          rabbitOperations.send( configuration.rabbitExchangeName, '', msg )
      }
  }
  </code></pre>
  
  <p>where:</p>
  
  <pre><code>@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.NONE )
  @ContextConfiguration( classes = Application )
  class BaseFlowIntegrationTest extends AbstractIntegrationTest {...}
  </code></pre>
  
  <p>And</p>
  
  <pre><code>@Category( InboundIntegrationTest )
  abstract class AbstractIntegrationTest extends Specification {...}
  </code></pre>