读书人

Spring3的测试框架使用来SpingMVC

发布时间: 2014-01-13 17:16:02 作者: rapoo

Spring3的测试框架——使用于SpingMVC

spring3.2终于出ga版了, 发现spring test的改进最大,添加了对 springmvc的测试

看了看文档,将使用方法整理如下:

1,可以使用 @WebAppConfiguration来标明是web应用测试, @ContextConfiguration来指定配置文件,其他的和测试相同

2,主要用到三个类: 1,MockMvc及MockMvcBuilders, 用来生成当前的测试环境,后者是生成MockMvc的

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2,MockMvcRequestBuilders , 模拟http请求

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 3,MockMvcResultMatchers ,对返回结果进行断言

3,这几个类都提供了链式操作,写代码的时候就很舒服了,代码也好看多了,下面就详细说说这几个类

3.1 MockMvcBuilders?

? ? ? ? ? ?这个类只有两个方法:DefaultMockMvcBuilder??webAppContextSetup(WebApplicationContext context) ?和?StandaloneMockMvcBuilder?standaloneSetup(Object... controllers)?,区别是 前者依赖Spring上下文,因此这个要加载配置文件

? ? ? ? ? ?StandaloneMockMvcBuilder 继承自 DefaultMockMvcBuilder?

? ? ? ? ? ?毕竟spring的配置文件中没有web.xml中那些filter的信息,无法完整模拟web环境, 因此 DefautlMockBuilder中有 addFilter(Filter filter) ?和 addFilters(Filter ...)两个方法,还有一个RequestBuilder,可以定制默认的request, 还有一些 alwaysExpect() ,alwaysDo等方法,用于添加默认断言

? ? ? ? ? 配置好Builder后,然后 调用build()方法 生成 MockMvc对象, 我们就可以用来测试了

3.2 MockMvcRequestBuilders?

? ? ? ? ? 这个类里面全是静态方法, 提供http操作方法如: get,post,delete,put,fileupload , 还有一个request方法,可以自己指定 前面那类型, 还有一个?asyncDispatch(MvcResult mvcResult),应该是异步执行,这个方法没用过,有时间看看是干嘛用的

? ? ? ? ? 这些方法返回的都是 RequestBuilder的子类, 针对不同http请求类型做了一些拓展,都是链式操作很方便的

3.2 MockMvcResultMatchers , 这个是对返回结果进行断言,也都是静态方法,链式操作, 这些方法返回的都是 ResultMather的子类

? ? ?提供的方法有:request(),handler(), model(),view(),flash(),forwardUrl(String expectUrl),redirectUrl(String expectUrl) ,status(),header(),content(),jsonPath() ,xpath(),cookie();

? ? ?jsonPath 和 xpath 是针对json和xml格式数据的,具体格式请参考?http://goessner.net/articles/JsonPath/ 这篇文章。

? ? jsonPath 操作依赖 jsonPath类, xpath没用过应该也有相关依赖吧

?

下面放一个测试例子吧?

public class SpringControllerTest {    @Test    public void json() throws Exception {        MockMvc mockMvc = standaloneSetup(new PersonController()).build();        mockMvc.perform(get("/person/Lee").accept(MediaType.APPLICATION_JSON))                .andExpect(status().isOk())                .andExpect(content().contentType("application/json"))                .andExpect(jsonPath("$.name").value("Lee")).andExpect(jsonPath("$.title").value("你好"));        mockMvc.perform(post("/p2/haha").param("title", "你好吧")).andExpect(status().is(302))                .andExpect(view().name("redirect:/hello")).andExpect(model().attribute("title", "你好吧"));        mockMvc.perform(get("/p2/haha2").param("title", "你好吧").accept(MediaType.APPLICATION_JSON))                .andExpect(status().isOk()).andExpect(jsonPath("$.data[0].fullName").value("宝马-进口-x6"));    }    @Controller    private class PersonController {        @RequestMapping(value = "/person/{name}")        public Object get(@PathVariable String name) {            Map person = new HashMap();            person.put("name", name);            person.put("title", "你好");            return ViewUtils.createJsonView(person);        }        @RequestMapping(value = "/p2/haha")        public Object post(String name, String title) {            Map person = new HashMap();            person.put("name", name);            person.put("title", title);            return ViewUtils.createView("redirect:/hello", person);        }        @RequestMapping(value = "/p2/haha2",produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")        @ResponseBody        public Object getjson(String name, String title) {            List<Car> cars = Lists.newArrayList();            cars.add(EntityFactory.createCar("宝马-进口-x6", "x6", null, "进口"));            JsonData data = new JsonData();            data.setData(BeanMapper.mapList(cars, CarDTO.class));            return data;        }    }}

?

?

?

读书人网 >VC/MFC

热点推荐