title: Differences between create MockMvc from WebApplicationContext and create MockMvc from your Controller instance?
body: <p>When i create MockMvc for restful-webservice testing with spring MVC 3.2.3 using WebApplicationContext like this:</p>
  
  <pre><code>@Autowired
  private WebApplicationContext wac;
  
  private MockMvc mockMvc;
  
  @Before
  public void setup() {
      this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
  }
  
  
  // ...
  
  }
  </code></pre>
  
  <p>Then when i run my test, it will fail with return code 404 and my log says that</p>
  
  <pre><code>Did not find handler method for [my/path]
  </code></pre>
  
  <p>But if i change to this way</p>
  
  <pre><code>public class MyWebTests {
  
  private MockMvc mockMvc;
  
  @Before
  public void setup() {
      this.mockMvc = MockMvcBuilders.standaloneSetup(new AccountController()).build();
  }
  
  // ...
  
  }
  </code></pre>
  
  <p>Then my test will run well without any error.</p>
  
  <p>The only difference i know is that if i use WebApplicationContext to create MockMvc then it will load my Spring Confuguration, while if i use Controller then it will not.</p>
  
  <p>What can possibly cause that error (did not find handler method ...) in my case?</p>
  
  <p>EDIT
  My test class</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration("classpath:restful-test-context.xml")
  @WebAppConfiguration
  public class TestSourceController {
  @Autowired
  private WebApplicationContext wac;
  
  private MockMvc mockMvc;
  private ObjectMapper mapper;
  
  @Autowired
  DeviceDAO deviceDAO;
  
  @AfterClass
  public static void tearDownAfterClass() throws Exception {
  }
  
  @Before
  public void setUp() throws Exception {
      Mockito.reset(deviceDAO);
      mockMvc = MockMvcBuilders.standaloneSetup(new SourceController()).build();
      mapper = new ObjectMapper();
  }
  
  @After
  public void tearDown() throws Exception {
  }
  
  @Test
  public void testGetAllDevice() throws UnsupportedEncodingException,
          Exception {
      Device device1 = new Device();
      device1.setId("switch1");
      device1.setIsRoot(true);
      device1.setName("switch1");
      device1.setStatus("ONLINE");
      device1.setType("SWITCH");
  
      Device device2 = new Device();
      device2.setId("switch2");
      device2.setIsRoot(true);
      device2.setName("switch2");
      device2.setStatus("OFFLINE");
      device2.setType("SWITCH");
  
      List&lt;Device&gt; devices = Arrays.asList(device1, device2);
  
      when(deviceDAO.getAllDevices()).thenReturn(devices);
  
      String expected = mapper.writeValueAsString(devices);
  
      String result = mockMvc
              .perform(get("/api/devices").accept(MediaType.APPLICATION_JSON))
              .andExpect(status().isOk()).andReturn().getResponse()
              .getContentAsString();
  
      verify(deviceDAO).getAllDevices();
  
      assertEquals(expected, result);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>My Controller</p>
  
  <pre><code>@Controller
  public class SourceController {
  
  private static final Logger logger = LoggerFactory.getLogger("SourceController");
  
  @RequestMapping(value = "/api/devices", method = RequestMethod.GET)
  @ResponseBody
  public List&lt;Device&gt; getAllDevice() {
      DeviceDAO deviceDAO = AppContext.getService(DeviceDAO.class,
              "deviceDAO");
      logger.info("The deviceDAO is" + deviceDAO.toString() + "!");
      return deviceDAO.getAllDevices();
  }
  }
  </code></pre>
  
  <p>My web.xml</p>
  
  <pre><code>&lt;web-app version="2.5"  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
  
  &lt;display-name&gt;Spring Web MVC Application&lt;/display-name&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;nhduc-training-app&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;nhduc-training-app&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;WEB-INF/nhduc-training-app-servlet.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  </code></pre>
  
  <p></p>
  
  <p>My restful-test-context.xml</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:mongo="http://www.springframework.org/schema/data/mongo"
  xmlns:oxm="http://www.springframework.org/schema/oxm"
  xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
      http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
      http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-1.5.xsd"&gt;
  &lt;context:annotation-config /&gt;
  &lt;mvc:annotation-driven /&gt;
  &lt;context:component-scan base-package="com.tma.nhduc.controller" /&gt;
  
  
  &lt;bean id="daoUtils" class="com.tma.nhduc.dao.DAOUtils" /&gt;
  
  &lt;bean id="counterService" class="com.tma.nhduc.dao.SequenceService" /&gt;
  
  &lt;bean id="portDAO" class="com.tma.nhduc.dao.PortDAO" /&gt;
  
  &lt;bean id="alarmDAO" class="com.tma.nhduc.dao.AlarmDAO" /&gt;
  
  &lt;bean id="deviceDAO" class="org.mockito.Mockito" factory-method="mock"&gt;
      &lt;constructor-arg value="com.tma.nhduc.dao.DeviceDAO"&gt;&lt;/constructor-arg&gt;
  &lt;/bean&gt;
  &lt;bean id="appContext" class="com.tma.nhduc.ctx.AppContext" /&gt;
  
  &lt;mongo:mongo host="localhost" port="27017" /&gt;
  &lt;mongo:db-factory dbname="nms" mongo-ref="mongo" /&gt;
  &lt;bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"&gt;
      &lt;constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p></p>