title: Spring Integration Tests - idiomatic way of overriding beans
body: <p>how can I override beans in Spring (Boot) Integration Tests the idiomatic way?</p>
  
  <p>Up until now I had source configuration like this:</p>
  
  <pre><code>@Configuration
  class ApplicationConfiguration {
    @Bean
    CarsRepository carsRepository() {
      // return some real sql db
    }
  }
  </code></pre>
  
  <p>And tests like this:</p>
  
  <pre><code>@SpringBootTest
  class ApplicationISpec extends Specification {
    @Configuration
    @Import(Application.class)
    static class TestConfig {
      @Bean
      @Primary
      CarsRepository testsCarsRepository() {
        // return some in-memory repository
      }
    }
  
    def "it can do stuff with cars"() {
      // do some REST requests to application and verify it works
      // there is no need to make real calls to real DB
    }
  }
  </code></pre>
  
  <p>First thing is that test bean <code>testsCarsRepository</code> method must differ than original one (which is not obvious, and there is no warning/error about it).
  But the final question is: what is the idiomatic way of overriding beans with Spring in integration tests?</p>
  
  <p>When I posted my WTF about method name on <a href="https://twitter.com/snicoll/status/921452796985118723" rel="nofollow noreferrer">Twitter</a> - <a href="https://stackoverflow.com/users/613628/stephane-nicoll">Stephane Nicoll</a> said the <code>@Primary</code> is not intended to be used for overriding beans in tests.</p>
  
  <p>So what is the preferred way of that?</p>