从Spring 3.2开始,有一种适当的方法可以轻松,优雅地进行测试。您将可以执行以下操作:
@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfiguration@ContextConfiguration("servlet-context.xml")public class SampleTests { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setup() { this.mockMvc = webAppContextSetup(this.wac).build(); } @Test public void getFoo() throws Exception { this.mockMvc.perform(get("/foo").accept("application/json")) .andExpect(status().isOk()) .andExpect(content().mimeType("application/json")) .andExpect(jsonPath("$.name").value("Lee")); }}有关更多信息,请访问http://blog.springsource.org/2012/11/12/spring-
framework-3-2-rc1-spring-mvc-test-
framework/



