title: SpringBootTest - how to replace one bean in runtime configuration?
body: <p>I am writing an integration test for Spring Boot application. All goes well as long as I'm using 100% runtime configuration for the test. But when I'm trying to provide just one custom bean for the bean, everything breaks.</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  public class CombinedControllerIntegrationTest2 {
  
  @TestConfiguration
  static class ContextConfiguration {
  
      @Bean
      @Primary
      public SolrDocumentTypeMapRepository solrDocumentTypeMapRepository() {
          LOG.debug("SolrDocumentTypeMapRepository is being initialized.");
  
  // etc.
  </code></pre>
  
  <p>code variant above is causing the real, runtime SolrDocumentTypeMapRepository to load. ContextConfiguration in my test class is ignored.</p>
  
  <p>If I try to use @Configuration instead of @TestConfiguration on my inner ContextConfiguration, execution falls to another extreme - it ends with</p>
  
  <blockquote>
    <p>org.springframework.context.ApplicationContextException: Unable to
    start EmbeddedWebApplicationContext due to missing
    EmbeddedServletContainerFactory bean.</p>
  </blockquote>
  
  <p>Because, apparently, the rest of configuration is not loaded.</p>
  
  <p>If I try to put</p>
  
  <blockquote>
    <p>@ContextConfiguration(classes =
    {CombinedControllerIntegrationTest2.ContextConfiguration.class,GatewayApplication.class})</p>
  </blockquote>
  
  <p>on my main testing class, it fails the same way as in #1 - i.e. my ContextConfiguration is ignored.</p>
  
  <p>Any ideas?</p>
  
  <p>P.S. I know I can use @MockBean (and this even works in other cases), but here, because at some point I have dependency on @PostConsruct methods in the main code, @MockBeans are useless.</p>