title: How to mock autowired dependencies in Spring Boot MockMvc Unit tests?
body: <p>I am expanding upon the basic Spring Boot examples, adding an "autowired" repository dependency to my controller. I would like to modify the unit tests to inject a Mockito mock for that dependency, but I am not sure how.</p>
  
  <p>I was expecting that I could do something like this:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = MockServletContext.class)
  @WebAppConfiguration
  public class ExampleControllerTest {
  
      private MockMvc mvc;
  
      @InjectMocks
      ExampleController exampleController;
  
      @Mock
      ExampleRepository mockExampleRepository;
  
      @Before
      public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
      mvc = MockMvcBuilders.standaloneSetup(new ExampleController()).build();
      }
  
      @Test
      public void getExamples_initially_shouldReturnEmptyList() throws Exception {
          mvc.perform(MockMvcRequestBuilders.get("/example").accept(MediaType.APPLICATION_JSON))
                  .andExpect(status().isOk())
                  .andExpect(content().string(equalTo("[]")));
      }
  }
  </code></pre>
  
  <p>but it doesn't inject the mock into the <code>MockMvc</code>. Can anyone explain how to do this with <code>@Autowired</code> dependencies, rather than constructor arguments?</p>