比Dao和Service的测试稍微复杂一点。还是先写一个BasicWebTest用来总体配置:
@WebAppConfiguration @ContextConfiguration(locations= {"classpath:spring/applicationContext.xml","classpath:spring/spring-servlet.xml"}) public class BasicWebTest extends AbstractTransactionalJUnit4SpringContextTests { @Autowired protected WebApplicationContext wac; protected MockMvc mvc; @Before public void setUp() { this.mvc=MockMvcBuilders.webAppContextSetup(wac).build(); } }
BasicWebTest 上面多了个 @WebAppConfiguration 注解,并且类中也多了一些方法、字段。直接复制吧。
测试用例如下:
package com.suyin.controller; import java.util.List; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.junit.Test; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import com.jayway.jsonpath.JsonPath; import com.suyin.BasicWebTest; import com.suyin.pojo.People; @SuppressWarnings("unchecked") public class PeopleControllerTest extends BasicWebTest { @Test public void queryAll() throws Exception { mvc.perform(MockMvcRequestBuilders.get("/people/queryAll")) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.view().name("people/list")) .andExpect(MockMvcResultMatchers.request().attribute("list", new MatcherAdapter() { @Override public void describeMismatch(Object item, Description mismatchDescription) { mismatchDescription.appendText("wrong list size"); } @Override public boolean matches(Object item) { List<People> list=(List<People>) item; return list.size()==4; } })); } @Test public void asynQueryAll() throws Exception { MvcResult mr=mvc.perform(MockMvcRequestBuilders.get("/people/asynQueryAll").content("{"id":1}")) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.jsonPath("$.id").exists()) .andReturn(); MockHttpServletResponse response=mr.getResponse(); MockHttpServletRequest request=mr.getRequest(); } @Test public void update1() throws Exception { mvc.perform(MockMvcRequestBuilders.post("/people/update").param("id", "29") .param("name", "Jack") .param("address", "japan") .param("age", "19")) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.view().name("people/detail")); } @Test public void update2() throws Exception { mvc.perform(MockMvcRequestBuilders.post("/people/update") //.param("name", "Jack") .param("address", "japan") .param("age", "19")) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.view().name("error")); } @Test public void add1()throws Exception { mvc.perform(MockMvcRequestBuilders.post("/people/add") .param("name", "JJJJ") .param("address", "") .param("age", "1") ) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.view().name("people/detail")); } @Test public void add2()throws Exception { mvc.perform(MockMvcRequestBuilders.post("/people/add") .param("address", "") .param("age", "1") ) .andExpect(MockMvcResultMatchers.status().isOk()) .andExpect(MockMvcResultMatchers.view().name("error")); } abstract class MatcherAdapter<T> implements Matcher<T>{ @Override public void describeTo(Description arg0) { // TODO Auto-generated method stub } @Override public void _dont_implement_Matcher___instead_extend_BaseMatcher_() { // TODO Auto-generated method stub } @Override public abstract void describeMismatch(Object item, Description mismatchDescription); @Override public abstract boolean matches(Object item) ; } }
认真说来关于Controller的Spring测试就这样了。在多就是关于MockMvc的API学习了。