title: Should Mockito be used with MockMvc's webAppContextSetup in Spring 4?
body: <p>I'm having difficulties getting Mockito and MockMvc working together when I use the webAppContextSetup together. I'm curious if it's because I'm mixing the two in a way they were never intended.</p>
  
  <p>Source: <a href="https://github.com/zgardner/spring-boot-intro/blob/master/src/test/java/com/zgardner/springBootIntro/controller/PersonControllerTest.java" rel="nofollow noreferrer">https://github.com/zgardner/spring-boot-intro/blob/master/src/test/java/com/zgardner/springBootIntro/controller/PersonControllerTest.java</a></p>
  
  <p>Here is the test I'm running:</p>
  
  <pre><code>package com.zgardner.springBootIntro.controller;
  
  import org.junit.Before;
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.mockito.InjectMocks;
  import org.mockito.Mock;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.support.DefaultListableBeanFactory;
  import org.springframework.boot.test.SpringApplicationConfiguration;
  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  import org.springframework.test.context.web.WebAppConfiguration;
  import org.springframework.test.web.servlet.MockMvc;
  import org.springframework.test.web.servlet.setup.MockMvcBuilders;
  import org.springframework.web.context.WebApplicationContext;
  
  import static java.lang.Math.toIntExact;
  import static org.hamcrest.Matchers.is;
  import static org.mockito.MockitoAnnotations.initMocks;
  import static org.mockito.Mockito.when;
  import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
  import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
  import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
  import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
  
  import com.zgardner.springBootIntro.Application;
  import com.zgardner.springBootIntro.service.PersonService;
  import com.zgardner.springBootIntro.model.PersonModel;
  
  @RunWith(SpringJUnit4ClassRunner.class)
  @SpringApplicationConfiguration(classes = Application.class)
  @WebAppConfiguration
  public class PersonControllerTest {
  
      private MockMvc mockMvc;
  
      @Autowired
      private WebApplicationContext webApplicationContext;
  
      @Autowired
      private DefaultListableBeanFactory beanFactory;
  
      @Mock
      private PersonService personService;
  
      @InjectMocks
      private PersonController personController;
  
      @Before
      public void setup() {
          initMocks(this);
  
          // beanFactory.destroySingleton("personController");
          // beanFactory.registerSingleton("personController", personController);
  
          mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
      }
  
      @Test
      public void getPersonById() throws Exception {
          Long id = 999L;
          String name = "Person name";
  
          when(personService.findById(id)).thenReturn(new PersonModel(id, name));
  
          mockMvc.perform(get("/person/getPersonById/" + id))
              .andDo(print())
              .andExpect(jsonPath("$.id", is(toIntExact(id))))
              .andExpect(jsonPath("$.name", is(name)));
      }
  }
  </code></pre>
  
  <p>I was expecting that when mockMvc performed the mock of that HTTP call, it would use the PersonController I defined in my test. But when I debug through, it's using the PersonController which was created by the SpringJunit4ClassRunner on the test boot up.</p>
  
  <p>I found two ways to get this to work:</p>
  
  <ol>
  <li>Inject the bean factory, remove the old personController singleton, and add my own. This is ugly, and I am not a fan.</li>
  <li>Wire everything up using the standaloneSetup instead of webAppContextSetup. I may do this instead as I don't have to touch the bean factory.</li>
  </ol>
  
  <p>Here are some different articles I've found that somewhat touch on the topic:</p>
  
  <ul>
  <li><a href="https://spring.io/guides/tutorials/bookmarks/" rel="nofollow noreferrer">Spring Tutorial - Building REST Services</a> This just autowires in the repos to clear out the data before the integration test takes place.</li>
  <li><a href="http://markchensblog.blogspot.com/2013/02/use-spring-mvc-test-framework-and.html" rel="nofollow noreferrer">Use Spring MVC Test framework and Mockito to test controllers</a> This uses Mockito along with webAppContextSetup, but this is in Spring 3. (I'm using Spring Boot)</li>
  <li><a href="https://stackoverflow.com/a/16207069/421923">Unable to mock Service class in Spring MVC Controller tests</a> This uses the standaloneSetup, which does work in my case too.</li>
  </ul>
  
  <p>Thoughts?</p>