基于JUnit测试Spring MVC的正确方法。(仅限Spring 3.2)
来源:互联网 发布:apache工作原理 编辑:程序博客网 时间:2024/05/16 17:20
package com.teamsun.test.JunitAnnocation;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.teamsun.core.linkplat.controller.RouteAction;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:jdbc-context.xml",
"classpath:springmvc-servlet.xml" })
public class TestController {
@Autowired
private WebApplicationContext wac;
@Autowired
private RouteAction routeaction;//你要测试的Controller
private MockMvc mockMvc;
@Before
public void setup() {
mockMvc = MockMvcBuilders.standaloneSetup(routeaction).build();
}
@Test
public void testFindPageUsers() throws Exception {
ResultActions ra = this.mockMvc.perform(MockMvcRequestBuilders
.post("/route/operate.do")
.accept(MediaType.APPLICATION_JSON).param("flag", "add")
.param("limit", "10"));
MvcResult mr = ra.andReturn();
String result = mr.getResponse().getContentAsString();
System.out.println("resutl==>"+result);
}
}
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.teamsun.core.linkplat.controller.RouteAction;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = { "classpath:jdbc-context.xml",
"classpath:springmvc-servlet.xml" })
public class TestController {
@Autowired
private WebApplicationContext wac;
@Autowired
private RouteAction routeaction;//你要测试的Controller
private MockMvc mockMvc;
@Before
public void setup() {
mockMvc = MockMvcBuilders.standaloneSetup(routeaction).build();
}
@Test
public void testFindPageUsers() throws Exception {
ResultActions ra = this.mockMvc.perform(MockMvcRequestBuilders
.post("/route/operate.do")
.accept(MediaType.APPLICATION_JSON).param("flag", "add")
.param("limit", "10"));
MvcResult mr = ra.andReturn();
String result = mr.getResponse().getContentAsString();
System.out.println("resutl==>"+result);
}
}
- 基于JUnit测试Spring MVC的正确方法。(仅限Spring 3.2)
- 基于Spring的Junit测试
- 基于spring 3 注解的junit测试
- Spring MVC(三)基于上一篇使用JUnit编写测试用例
- Spring的Junit测试
- Spring的Junit测试
- Spring MVC Junit测试实例 附:读取WEB-INF下配置文件的方法
- Spring MVC Junit测试实例 附:读取WEB-INF下配置文件的方法
- Spring MVC Junit测试实例 附:读取WEB-INF下配置文件的方法
- Spring MVC Junit测试实例 附:读取WEB-INF下配置文件的方法
- Spring MVC 测试方法
- Spring MVC参数化测试 - Junit Parameterized
- spring mvc 使用junit 测试 controller
- 基于Spring的可复用的Junit测试类的设计
- 基于spring的junit单元测试
- spring下的junit测试
- Apache Ant+Junit对Java(Spring MVC(基于注解))工程进行测试(二)
- spring mvc+junit
- 基于LinkedHashMap实现LRU缓存调度算法原理及应用
- 科技的发展让公司“死”得越来越快
- CL邀请码
- 求 数组里 和为s的所有组合
- MVC和三层架构
- 基于JUnit测试Spring MVC的正确方法。(仅限Spring 3.2)
- IOS笔记——UIScrollView自定义滚动条
- 颜色空间系列1: RGB和CIEXYZ颜色空间的转换及相关优化
- 阿里巴巴搜索系统总结(三)
- android 使用MediaStore检索图像
- Wp7 App开发性能优化方法和指标
- 同步和互斥的POSIX支持(读写锁、信号量和记录锁)
- JAVA学习之路——第二天
- Loadrunner中参数和变量的使用