title: Injecting Mockito Mock objects using Spring JavaConfig and @Autowired
body: <p>I'm trying to replace an <code>@Autowired</code> object with a Mockito mock object. The usual way of doing this was with xml using Springockito:</p>
  
  <pre><code>&lt;mockito:mock id="SomeMock" class="com.package.MockInterface" /&gt;
  </code></pre>
  
  <p>Currently I'm trying to move over to using Spring's JavaConfig to do the job. All of a sudden the Java expressions are a whole lot more verbose than xml:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration
  public class MyTestClass {
  
      @Configuration
      static class Config {
          @Bean
          public MockInterface somethingSpecial() {
              return Mockito.mock(MockInterface.class);
          }
      }
  
      @Autowired MockInterface mockObj;
  
      // test code
  }
  </code></pre>
  
  <p>I discovered a library called Springockito-annotations, which allows you to do the following:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(loader=SpringockitoContextLoader.class)
  public class MyTestClass {
  
      @Autowired @ReplaceWithMock MockInterface mockObj;
  
      // test code
  }
  </code></pre>
  
  <p>Clearly, a whole lot prettier :) The only problem is that this context loader doesn't allow me to use <code>@Configuration</code> and JavaConfig for other beans (if I do, Spring complains that there are no candidates that match those autowired fields).</p>
  
  <p>Do you guys know of a way to get Spring's JavaConfig and Springockito-annotations to play nice? Alternatively, is there another shorthand for creating mocks?</p>
  
  <p>As a nice bonus, using Springockito and xml config, I was able to mock out concrete classes without providing autowiring candidates to its dependencies (if it had any). Is this not possible without xml?</p>