title: Configure @MockBean component before application start
body: <p>I have a Spring Boot 1.4.2 application. Some code which is used during startup looks like this:</p>
  
  <pre><code>@Component 
  class SystemTypeDetector{
      public enum SystemType{ TYPE_A, TYPE_B, TYPE_C }
      public SystemType getSystemType(){ return ... }
  }
  
  @Component 
  public class SomeOtherComponent{
      @Autowired 
      private SystemTypeDetector systemTypeDetector;
      @PostConstruct 
      public void startup(){
          switch(systemTypeDetector.getSystemType()){   // &lt;-- NPE here in test
          case TYPE_A: ...
          case TYPE_B: ...
          case TYPE_C: ...
          }
      }
  }
  </code></pre>
  
  <p>There is a component which determines the system type. This component is used during startup from other components. In production everything works fine.</p>
  
  <p>Now I want to add some integration tests using Spring 1.4's <code>@MockBean</code>.</p>
  
  <p>The test looks like this:</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest(classes = MyWebApplication.class, webEnvironment = RANDOM_PORT)
  public class IntegrationTestNrOne {
      @MockBean 
      private SystemTypeDetector systemTypeDetectorMock;
  
      @Before 
      public void initMock(){
         Mockito.when(systemTypeDetectorMock.getSystemType()).thenReturn(TYPE_C);
      }
  
      @Test 
      public void testNrOne(){
        // ...
      }
  }
  </code></pre>
  
  <p>Basically the mocking works fine. My systemTypeDetectorMock is used and if I call <code>getSystemType</code> -> <code>TYPE_C</code> is returned.</p>
  
  <p><strong>The problem</strong> is that the application doesn't start. Currently springs working order seems to be:</p>
  
  <ol>
  <li>create all Mocks (without configuration all methods return null)</li>
  <li>start application</li>
  <li>call @Before-methods (where the mocks would be configured)</li>
  <li>start test</li>
  </ol>
  
  <p>My problem is that the application starts with an uninitialized mock. So the call to <code>getSystemType()</code> returns null.</p>
  
  <p>My question is: How can I configure the mocks <em>before</em> application startup?</p>
  
  <p><strong>Edit:</strong> If somebody has the same problem, one <em>workaround</em> is to use <code>@MockBean(answer = CALLS_REAL_METHODS)</code>. This calls the real component and in my case the system starts up. After startup I can change the mock behavior.</p>