Spring 4.0 JUnit简单的Controller测试
作者:网络转载 发布时间:[ 2015/7/2 16:04:47 ] 推荐标签:单元测试
比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学习了。
相关推荐
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11