如何在springMVC 中对REST服务使用spring mockmvc post做测试

使用Spring&MVC&搭建Rest服务
使用Spring MVC 搭建Rest服务
本文由大关总结整理所得,不保证内容的正确性,转载请标明出处!
Rest(Representational State
Transfer,表述状态转移),是一种基于Http协议的,能够快速开发网络服务程序,并且提高网络服务系统伸展性的设计和开发方式。Rest的两端可以是不同构的编程和程序运行环境,Rest通过Http协议将通信的两端进行连接,在服务两端通过对Http协议的使用,最终完成数据的翻译和转换。
Rest有几个重要的特点,使得Rest能够在服务搭建上占有一定的优势。首先,Rest以一切皆资源的方式来看待所有的web提供的服务(包括web服务本身,以及web服务中某个具体的服务应用),所有的资源都采用URI进行定位。其次,Rest通过对Http中相应字段属性设置,确定客户端和服务端的缓存策略,在一定程度上,可以缓解服务器和客户端的压力(Rest服务被认为是无状态的web服务),最后,Rest通过对Http消息体中(MIME)Content-Type的内容不同,可以采用不同处理策略对消息分解,获取需要的信息,此外Rest还有很多优势,具体可以参考:Architectural
Styles and the Design of Network-based Software
Architectures这篇论文,文中详细描述了Rest服务的推导过程。
要想使用Rest必须要了解Http服务中定义的几个关键的方法。GET,用于从服务端获取信息,但并不更改服务端的任何状态;POST,用于提交创建的信息,一般更改服务端的数据状态,并且返回新创建对象的ID(一般来说,为了保证ID的唯一性,ID的值需要由服务端来确定,因此在创建结束后会将新分配的ID反馈给客户端);PUT,用于更改服务端某个对象的状态(一般被认为是Update),服务端状态受到影响,不需要返回影响后的结果;DELETE,删除服务端的对象,服务端的状态发生改变,可以返回删除后的对象,也可以不返回。(Http还有其他的方法,但在Rest中不是很常用,可以查看RFC2616)。这些方法仅是意义上这样说,但是并不是一定要这用,具体的方法对应的动作完全是服务两端确定的。
在JAVA中搭建Rest服务有很多种,其中JAX-RS是JAVA定义的Rest服务的API,可以使用Apache实现的jersy搭建Rest服务。在Spring
MVC中也提供了搭建Rest服务的方法,下文主要探讨如何使用Spring MVC搭建Rest服务。
下面的内容需要您对如下内容有所了解:
(1)& 会使用Spring框架,理解依赖注入原理,理解Spring的基本配置。
使用过Java相关的Web容器(例如,Tomcat或Apache等,这里使用的是Jetty,原理相同)
(3)& 会使用一款OXM框架(例如:Castor或JAXB,这里使用的是JAXB2)
(4)& 对什么是Http服务和什么是Rest服务略有了解
如果您完全满足上面的条件,那么理解下面的内容就根本不成问题,如果您对有些内容不是很清楚,可以先看整个服务的流程,具体细节可以略看。
1.& Rest功能说明
下面我们将开始使用Spring
MVC的方法搭建一个Rest服务,在这个服务中,服务器端维护了一个关于Student的集合,客户端可以通过连接到服务端,向服务端添加、删除、查找和更新学生信息等操作。
2.& 学生数据结构(bean)
通过对Rest功能的分析,可以看出,服务端有两个关键的数据结构,一个是用来表示学生信息的Student类,另外一个是用来表示学生集合的StudentList类。下面我们通过schema定义这两个数据结构。
StudentLists.xsd
&?xml version="1.0"
encoding="UTF-8"?&
&xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"&
&xsd:complexType
name="student"&
&xsd:sequence&
&&&&&&&&&&&&&
&xsd:element name="name"
type="xsd:string" minOccurs="1"/&
&&&&&&&&&&&&&
&xsd:element name="age"
type="xsd:int"/&
&&&&&&&&&&
&/xsd:sequence&
&&&&&&&&&&
&xsd:attribute name="id"
type="xsd:string" use="required"/&
&/xsd:complexType&
&xsd:element
name="studentList"&
&xsd:complexType&
&&&&&&&&&&
&xsd:sequence&
&&&&&&&&&&&&&
&xsd:element name="students"
type="student"&
maxOccurs="unbounded"/&
&&&&&&&&&&
&/xsd:sequence&
&/xsd:complexType&
&/xsd:element&
&/xsd:schema&
可以看出,schema中定义了两个结构,一个是学生类型,一个是学生集合类型。学生类型包括两个元素(分别是name和age)和一个属性(id)。学生集合类型中仅包含一个元素(students,是一个学生类型的集合)。
通过使用xjc生成java类,xjc命令如下:
I:\programs\eclipse\SpringMVCRestTest\src&xjc -p
com.upc.upcgrid.guan.springMvcR
estTest.bean.student StudentLists.xsd
xjc(xml to java
compiler),可以将schema转换成java类,-p后面的两个参数分别是转换后的包名,以及需要转换的schema文件名字。此时,你刷新你的服务,可以看到xjc已经生成了Student类和StudentLists类,此外还生成了一个ObjectFactory类,这里我们不需要ObjectFactory类,因此直接将这个类删除,之后在Student类的声明前增加一条语句,使得Student类声明部分如下:
@XmlType(name = "student", propOrder = {
&&& "name",
@XmlRootElement(name="student")
public class Student {
3.& 学生管理类
学生的基本结构已经搭建完成,现在需要提供一个管理类,用于来管理学生集合(注意,正常情况下,应当将学生数据存入数据库,这里为了简化,仅将这些信息出入一个Map中进行管理)。学生管理类维护这学生集合,以及基于集合之上的操作。
StudentManager.java
@Component
public class StudentManager {
Map&String, Student& students =
new ConcurrentHashMap&String,
Student&();
public synchronized void addStudent(Student
if(students.containsKey(student.getId()))
&&&&&&&&&&
students.put(student.getId(), student);
public synchronized void deleteStudent(String
if(students.containsKey(id))
&&&&&&&&&&
students.remove(id);
public synchronized void updateStudent(String
id,Student student){
deleteStudent(id);
if(student.getId()==null ||
student.getId().equals(""))
&&&&&&&&&&
student.setId(id);
addStudent(student);
public synchronized Student getStudent(String
return students.get(id);
public synchronized StudentList getStudent() {
StudentList sl = new StudentList();
for(String key : students.keySet())
&&&&&&&&&&
sl.getStudents().add(students.get(key));
可以看出,我们将StudentManager标记成component,以便Spring能够将这个类的实例注入到使用它的类中(注意,这个类必须是一个单例模式,因为这个类中维护着学生的集合)。此外,StudentManager中的所有方法都标记成了同步方法,并且Map集合也是一个同步集合。StudentManager提供了对学生集合Map的基本操作。
4.& Rest服务实现
下面提供了使用Spring MVC实现Rest服务的方法。
@Controller
@RequestMapping(value="/students")
public class StudentServer {
private StudentM
@RequestMapping(method=RequestMethod.POST)
@ResponseBody
public String createStudent(@RequestBody Student
manager.addStudent(student);
return student.getId();
@RequestMapping(value="/{id}",method=RequestMethod.GET)
@ResponseBody
public Student getStudent(@PathVariable String id){
return manager.getStudent(id);
@RequestMapping(method=RequestMethod.GET)
@ResponseBody
public StudentList getStudent(){
return manager.getStudent();
@RequestMapping(value="/{id}",method=RequestMethod.PUT)
@ResponseBody
public void updateStudent(@RequestBody Student
student,@PathVariable String id){
manager.updateStudent(id, student);
@RequestMapping(value="/{id}",method=RequestMethod.DELETE)
@ResponseBody
public void deleteStudent(@PathVariable String
manager.deleteStudent(id);
@Autowired
public void setManager(StudentManager manager) {
this.manager =
用Control标记,Spring会认为这是一个Web服务,使用RequestMapping指明了服务映射的URI内容和映射的Http请求的方法。使用PathVariable可以从URI中获取参数,使用RequestBody,Spring会将Http消息体内部的数据使用配置策略转换成一个对象(参考5.
Spring的配置),使用ResponseBody,Spring会将返回值转换成消息体需要的格式(参考5.Spring的配置)。
5.& Spring的配置
现在,我们需要对Spring进行基本的配置,以便Spring能够使用正确的方式对接收到(发送出)的Http请求(相应)使用正确的方法处理(生成)消息体。我们的基本策略是,如果接收到的是XML文档(即Content-type是***/xml类型),则使用JAXB2将消息体转换成对象,否则将消息体作为普通文本进行处理;如果要发送的消息是复杂对象(不是简单类型,例如:String、Integer、void等),则将消息使用JAXB2进行编组,否则仅生成文本消息。
Spring的配置如下:
SpringConfig.java
@Configuration
public class SpringConfig {
private Jaxb2M
public @Bean Jaxb2Marshaller
jaxb2Marshaller()//配置JAXB2Context
Jaxb2Marshaller marshaller = new
Jaxb2Marshaller();//创建JAXB上下文环境
marshaller.setClassesToBeBound(Student.class,StudentList.class);//映射的xml类放入JAXB环境中&&&&
this.marshaller =
public @Bean AnnotationMethodHandlerAdapter
annotationMethodHandlerAdapter()
AnnotationMethodHandlerAdapter adapter = new
AnnotationMethodHandlerAdapter();//创建消息体转换器
HttpMessageConverter&?&[] converters
HttpMessageConverter&?&[2];//创建转换数组
StringHttpMessageConverter stringConverter = new
StringHttpMessageConverter();//创建字符转换器
MarshallingHttpMessageConverter marshallerConverter = new
MarshallingHttpMessageConverter();//创建xom转换器
marshallerConverter.setMarshaller(marshaller);//设置marshaller
marshallerConverter.setUnmarshaller(marshaller);//设置unmarshaller
//将两个转换器放入列表
converters[0] = (stringConverter);
converters[1] = (marshallerConverter);
//将转换器列表赋值给消息体转换器
adapter.setMessageConverters(converters);
//返回消息体转换器&&
SpringConfig中先配置了JAXB2,在JAXB2中需要指定在2学生数据结构中生成的两个类,因此JAXB环境会知道如何将Student和StudentList编组和解组。
SpringConfig中还配置了一个Adapter,这个转换器是Spring用来转换消息体时使用的,在这里,我们为Adapter配置了两个Converter,一个使用来处理XML的Marshaller的Converter,一个是用来处理普通类型String的Converter。因此Spring会根据情况,选择正确的Converter处理消息体。(注意,只要你提供了一个Adapter,Spring就会使用你提供的这个Adapter处理消息体,如果你没有提供,Spring将会使用默认的Adapter处理消息体,由于对于XML类型(或者JSON类型)的数据必须手动提供Converter,所以这里不能使用默认的Converter)。
之后是Spring的配置文件:
rest-servlet.xml
&?xml version="1.0"
encoding="UTF-8"?&
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
&&&&&&&&&&
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
&&&&&&&&&&
http://www.springframework.org/schema/aop
&&&&&&&&&&
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
&&&&&&&&&&
http://www.springframework.org/schema/tx
&&&&&&&&&&
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
&&&&&&&&&&
http://www.springframework.org/schema/context
&&&&&&&&&&
http://www.springframework.org/schema/context/spring-context-2.5.xsd"&
&&&&&&&&&&
&context:annotation-config/&
&&&&&&&&&&
&context:component-scan
base-package="com.upc.upcgrid.guan"/&&&&&&&&&&&&&&&&&
Spring的配置文档中,仅让Spring环境扫描标有标记类的包。
&?xml version="1.0"
encoding="UTF-8"?&
&web-app version="2.5"
xmlns="/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="/xml/ns/javaee
/xml/ns/javaee/web-app_2_5.xsd"&
&servlet-name&rest&/servlet-name&
&servlet-class&org.springframework.web.servlet.DispatcherServlet&/servlet-class&
&init-param&
&&&&&&&&&&
&param-name&contextConfigLocation&/param-name&
&&&&&&&&&&
&param-value&/conf/rest-servlet.xml&/param-value&
&/init-param&
&load-on-startup&1&/load-on-startup&
&/servlet&
&servlet-mapping&
&servlet-name&rest&/servlet-name&
&url-pattern&/rest/*&/url-pattern&
&/servlet-mapping&&&
&/web-app&
Spring使用DispatcherServlet完成URI任务的匹配和分发,在创建DispatcherServlet的时候,提供了Spring配置文件的路径。
6.& Jetty服务器的配置和启动
我们这里使用较轻量级的Jetty服务器,以嵌入式的方式启动Spring服务。Jetty服务要比Tomcat小的多,并且无需安装,可以嵌入到程序中执行。下面给出Jetty的启动过程。
JettyServerStart.java
public class JettyServerStart {
public static void main(String[] args) {
&Server server= new
Server();//创建jetty web容器(这与tomcat容器类似)
server.setStopAtShutdown(true);//在退出程序是关闭服务
//创建连接器,每个连接器都是由IP地址和端口号组成,连接到连接器的连接将会被jetty处理
//第一个连接器的连接方式为http://202.194.158.128:8586
Connector connector = new
SelectChannelConnector();//创建一个连接器
connector.setPort(8586);//连接的端口号,(tomcat下)一般是8080,这里根据服务需求进行设置
connector.setHost("202.194.158.128");//ip地址
server.addConnector(connector);//添加连接
//创建本地连接器,连接方式为http://localhost:8585
Connector connectorLocal = new SelectChannelConnector();
connectorLocal.setPort(8585);
connectorLocal.setHost("localhost");
server.addConnector(connectorLocal);
//配置rest服务
WebAppContext context = new WebAppContext();//创建服务上下文
context.setContextPath("/SpringMVCRestTest");//访问服务路径
http://{ip}:8568/SpringMVCRestTest
context.setConfigurationDiscovered(true);
context.setDescriptor(System.getProperty("user.dir")+File.separator+"conf"+File.separator+"web.xml");//指明服务描述文件,就是web.xml
context.setResourceBase(System.getProperty("user.dir"));//指定服务的资源根路径,配置文件的相对路径与服务根路径有关
server.setHandler(context);//添加处理
&&&&&&&&&&
server.start();//开启服务
&&&&&&&&&&
server.join();
} catch (Exception e) {
&&&&&&&&&&
e.printStackTrace();
&&&&&&&&&&
System.exit(1);
}//开启服务
现在,我们执行main方法,会的到如下输出:
21:22:07.609:INFO::jetty-7.3.0.v
21:22:07.796:INFO::NO JSP Support for
/SpringMVCRestTest, did not find
org.apache.jasper.servlet.JspServlet
21:22:07.921:INFO::started
o.e.j.w.WebAppContext{/SpringMVCRestTest,file:/I:/programs/eclipse/SpringMVCRestTest/}
21:22:08.093:INFO:/SpringMVCRestTest:Initializing Spring
FrameworkServlet 'rest'
org.springframework.web.servlet.FrameworkServlet
initServletBean
信息: FrameworkServlet 'rest': initialization
org.springframework.context.support.AbstractApplicationContext
prepareRefresh
信息: Refreshing WebApplicationContext for namespace
'rest-servlet': startup date [Fri Jun 24 21:22:08 CST 2011]; root
of context hierarchy
org.springframework.beans.factory.xml.XmlBeanDefinitionReader
loadBeanDefinitions
信息: Loading XML bean definitions from
ServletContext resource [/conf/rest-servlet.xml]
org.springframework.beans.factory.support.DefaultListableBeanFactory
preInstantiateSingletons
信息: Pre-instantiating singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory@7736bd:
defining beans
[org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,springConfig,studentManager,studentServer,jaxb2Marshaller,annotationMethodHandlerAdapter];
root of factory hierarchy
org.springframework.oxm.jaxb.Jaxb2Marshaller
createJaxbContextFromClasses
信息: Creating JAXBContext with classes to be bound
com.upc.upcgrid.guan.springMvcRestTest.bean.student.Student,class
com.upc.upcgrid.guan.springMvcRestTest.bean.student.StudentList]
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
registerHandler
信息: Mapped URL path [/students/{id}] onto handler
[com.upc.upcgrid.guan.springMvcRestTest.spring.StudentServer@497904]
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
registerHandler
信息: Mapped URL path [/students/{id}.*] onto handler
[com.upc.upcgrid.guan.springMvcRestTest.spring.StudentServer@497904]
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
registerHandler
信息: Mapped URL path [/students/{id}/] onto handler
[com.upc.upcgrid.guan.springMvcRestTest.spring.StudentServer@497904]
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
registerHandler
信息: Mapped URL path [/students] onto handler
[com.upc.upcgrid.guan.springMvcRestTest.spring.StudentServer@497904]
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
registerHandler
信息: Mapped URL path [/students.*] onto handler
[com.upc.upcgrid.guan.springMvcRestTest.spring.StudentServer@497904]
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping
registerHandler
信息: Mapped URL path [/students/] onto handler
[com.upc.upcgrid.guan.springMvcRestTest.spring.StudentServer@497904]
org.springframework.web.servlet.FrameworkServlet
initServletBean
信息: FrameworkServlet 'rest': initialization
completed in 969 ms
21:22:09.125:INFO::Started
SelectChannelConnector@202.194.158.128:8586
21:22:09.125:INFO::Started
SelectChannelConnector@localhost:8585
从输出的内容可以看出,Spring服务已经正常启动,并且Jetty将会在本地的两个端口进行监听。
7.& 编写客户端测试程序
Rest的客户端可以使用Spring的RestTemplate进行编写。为了给使用这屏蔽掉我们后台与服务器进行通信的复杂操作,我们编写了一个RestUtility,这个类主要完成与服务器的通信。
RestUtility.java
public class RestUtility {
private static String url =
"http://202.194.158.128:8586/SpringMVCRestTest/rest/students/";&&&
private static RestTemplate restTemplate;
createRestTemplate();
public static RestTemplate
getRestTemplate(){&&&
return restTemplate;
private static void createRestTemplate() {
restTemplate = new RestTemplate();
List&HttpMessageConverter&?&&
converters = new
ArrayList&HttpMessageConverter&?&&();//消息体转换器列表
MarshallingHttpMessageConverter marshalConverter = new
MarshallingHttpMessageConverter();//xom类型的消息体转换器
Jaxb2Marshaller marshaller = new
Jaxb2Marshaller();//创建JAXB2类型的xom环境
marshaller.setClassesToBeBound(Student.class,StudentList.class);//将类绑定到JAXB2
marshalConverter.setMarshaller(marshaller);//设置编组器
marshalConverter.setUnmarshaller(marshaller);//设置解组器
converters.add(marshalConverter);//将xom消息体转换器添加到列表&
converters.add(new StringHttpMessageConverter());
restTemplate.setMessageConverters(converters);//将转换器列表放入RestTemplate
public static void addStudent(Student student)
throws RestClientException, URISyntaxException{
System.out.println(restTemplate.postForObject(new
URI(url), student,String.class));
public static Student getStudent(String id){
return restTemplate.getForObject(url+"{id}",
Student.class, id);
public static void updateStudent(String
id,Student student){
restTemplate.put(url+"{id}", student, id);
public static void deleteStudent(String
restTemplate.delete(url+"{id}", id);
public static StudentList getAllStudents()
throws RestClientException, URISyntaxException{
return restTemplate.getForObject(new
URI(url),StudentList.class);
这个类主要使用RestTemplate对服务器进行通信的。
最后给出一个测试程序,并给出结果.
public class RestClient {
public static void main(String[] args)
throws RestClientException, URISyntaxException {
Student student = new Student();
student.setAge(20);
student.setName("Mary");
student.setId("");&
RestUtility.addStudent(student);&&&&&&
student.setAge(21);
student.setName("Lucy");
student.setId("");
RestUtility.addStudent(student);
student = RestUtility.getStudent("");
System.err.println(student.getName());
StudentList sl = RestUtility.getAllStudents();
for(Student s : sl.getStudents())
&&&&&&&&&&
System.err.println(s.getName());
在测试程序中,我们先创建了两个学生,并将两个学生添加到远程,之后从远程查询了一个学生,并输出信息,然后查询了所有学生,输出信息。整个过程没有对错误进行处理。
执行输出:
org.springframework.oxm.jaxb.Jaxb2Marshaller
createJaxbContextFromClasses
信息: Creating JAXBContext with classes to be bound
com.upc.upcgrid.guan.springMvcRestTest.bean.student.Student,class
com.upc.upcgrid.guan.springMvcRestTest.bean.student.StudentList]
8.& 程序结构
Spring3.0官方文档
&&& Spring
MVC与JAX-RS对比:
教材:Restful Java with JAX-RS
论文:Architectural Styles and the Design of Network-based Software
Architectures
源码下载:
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。SPRING-MVC(112)
JUNIT(5)
spring 集成测试中 对mock 的集成实在是太棒了!但是使用请注意一下3个条件。
junit 必须使用4.9以上同时您的框架必须是用spring mvc&spring 3.2以上才完美支持
目前使用spring MVC 取代struts2 的很多,spring MVC 的各种灵活让人无比销魂!所以使用spring MVC吧!
以前在对接口(主要是java服务端提供的接口(一般是:webService,restful))进行测试的中 一般用以下俩种方法。测试流程如图:
1 直接使用httpClient&
& & 这方法各种麻烦
2 使用Spring 提供的RestTemplate
& & 错误不好跟踪,必须开着服务器
使用mockMVC都不是问题了看使用实例:
使用事例如下:父类
Java代码&&
import&org.junit.runner.RunW&&
import&org.springframework.beans.factory.annotation.A&&
import&org.springframework.test.context.ContextC&&
import&org.springframework.test.context.junit4.SpringJUnit4ClassR&&
import&org.springframework.test.context.web.WebAppC&&
import&org.springframework.web.context.WebApplicationC&&
@WebAppConfiguration&&
@ContextConfiguration(locations&=&{&&classpath:applicationContext.xml&,&&&&
&classpath:xxxx-servlet.xml&&})&&
public&class&AbstractContextControllerTests&{&&
&&&&@Autowired&&
&&&&protected&WebApplicationContext&&&
import&org.junit.B&&
import&org.junit.T&&
import&org.junit.runner.RunW&&
import&org.springframework.beans.factory.annotation.A&&
import&org.springframework.http.MediaT&&
import&org.springframework.test.context.junit4.SpringJUnit4ClassR&&
import&org.springframework.test.web.servlet.MockM&&
import&org.springframework.test.web.servlet.setup.MockMvcB&&
import&com.conlect.oatos.dto.status.REST&&
import&com.qycloud.oatos.server.service.PersonalDiskS&&
@RunWith(SpringJUnit4ClassRunner.class)&&
public&class&PersonalDiskMockTests&extends&AbstractContextControllerTests&{&&
&&&&private&static&String&URI&=&RESTurl.searchPersonalF&&
&&&&private&MockMvc&mockM&&
&&&&private&String&json&=&{\&entId\&:1234,\&userId\&:1235,\&key\&:\&new\&}&;&&
&&&&@Autowired&&
&&&&private&PersonalDiskService&personalDiskS&&
&&&&@Before&&
&&&&public&void&setup()&{&&
&&&&&&&&&&
&&&&&&&&this.mockMvc&=&MockMvcBuilders.standaloneSetup(personalDiskService).build();&&
&&&&public&void&readJson()&throws&Exception&{&&
&&&&&&&&this.mockMvc.perform(&&
&&&&&&&&&&&&&&&&post(URI,&&json&).characterEncoding(&UTF-8&)&&
&&&&&&&&&&&&&&&&&&&&.contentType(MediaType.APPLICATION_JSON)&&
&&&&&&&&&&&&&&&&&&&&.content(json.getBytes()))&&
&&&&&&&&&&&&&&&&.andExpect(content().string(&Read&from&JSON:&JavaBean&{foo=[bar],&fruit=[apple]}&)&&
&&&&&&&&&&&&&&&&&&&&);&&
上面是简单的例子,实际使用起来可以稍做修改。记得导入spring -test jar 包。无需额外配置(是不是很方便!)
当然和junit 的assert 一起用的话效果更好。下面贴点例子。copy就能跑。
Java代码&&
package&com.qycloud.oatos.server.test.mockmvcT&&
import&static&org.junit.Assert.&&
import&static&org.springframework.test.web.servlet.request.MockMvcRequestBuilders.&&
import&java.io.UnsupportedEncodingE&&
import&java.lang.reflect.F&&
import&org.springframework.http.MediaT&&
import&org.springframework.test.web.servlet.MockM&&
import&com.conlect.oatos.mC&&
import&com.conlect.oatos.dto.status.ErrorT&&
import&com.conlect.oatos.http.PojoM&&
public&class&MockUtil&{&&
&&&&public&static&String&mock(MockMvc&mvc,&String&uri,&String&json)&&
&&&&&&&&&&&&throws&UnsupportedEncodingException,&Exception&{&&
&&&&&&&&return&mvc&&
&&&&&&&&&&&&&&&&.perform(&&
&&&&&&&&&&&&&&&&&&&&&&&&post(uri,&&json&).characterEncoding(&UTF-8&)&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&.contentType(MediaType.APPLICATION_JSON)&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&.content(json.getBytes())).andReturn()&&
&&&&&&&&&&&&&&&&.getResponse().getContentAsString();&&
&&&&public&static&&T&&void&check(String&re,&Class&T&&object,String&testName)&{&&
&&&&&&&&System.out.println(re);&&
&&&&&&&&if&(ErrorType.error500.toString().equals(re))&{&&
&&&&&&&&&&&&System.out.println(&-----该接口测试失败:-----&&&
&&&&&&&&&&&&&&&&&&&&+&testName);&&
&&&&&&&&&&&&fail(re);&&
&&&&&&&&}&else&if&(CommConstants.OK_MARK.toString().equals(re))&{&&
&&&&&&&&&&&&System.out.println(&-----该接口测试成功:-----&&&
&&&&&&&&&&&&&&&&&&&&+&testName);&&
&&&&&&&&}else{&&
&&&&&&&&&&&&System.out.println(&-----re-----&:&+re);&&
&&&&&&&&}&&
&&&&&&&&if&(object&!=&null)&{&&
&&&&&&&&&&&&if&(re.contains(&:&))&{&&
&&&&&&&&&&&&&&&&try&{&&
&&&&&&&&&&&&&&&&&&&&T&t&=&PojoMapper.fromJsonAsObject(re,&object);&&
&&&&&&&&&&&&&&&&&&&&System.out.println(&-----该接口测试成功:-----&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&+&testName);&&
&&&&&&&&&&&&&&&&}&catch&(Exception&e)&{&&
&&&&&&&&&&&&&&&&&&&&System.out.println(&-----该接口测试失败:-----&&&
&&&&&&&&&&&&&&&&&&&&&&&&&&&&+&testName);&&
&&&&&&&&&&&&&&&&&&&&fail(e.getMessage());&&
&&&&&&&&&&&&&&&&}&&
&&&&&&&&&&&&}&&
&&&&&&&&}&&
&&&&public&static&&T&&Long&updateVersion(MockMvc&mockMvc,&String&url,&&
&&&&&&&&&&&&Long&fileId,&Class&T&&class1)&throws&UnsupportedEncodingException,&Exception&{&&
&&&&&&&&&&
&&&&&&&&String&re&=&mock(mockMvc,&url,&fileId+&&);&&
&&&&&&&&&&
&&&&&&&&T&dto&=&PojoMapper.fromJsonAsObject(re,&class1);&&
&&&&&&&&&&
Long&version&=&Long.parseLong(dto.getClass().getMethod(&getVersion&).invoke(dto).toString());&&&&&
&&&&&&&&System.out.println(&version&=&&+version);&&
&&&&&&&&&&
&&&&&&&&return&&&
&&&&&&&&&&
&使用如下:
Java代码&&
@RunWith(SpringJUnit4ClassRunner.class)&&
public&class&PersonalDiskMockTests&extends&AbstractContextControllerTests&{&&
&&&&private&MockMvc&mockM&&
&&&&private&static&Long&entId&=&1234L;&&
&&&&private&static&Long&adminId&=&1235L;&&
&&&&@Autowired&&
&&&&private&PersonalDiskService&personalDiskS&&
&&&&@Before&&
&&&&public&void&setup()&{&&
&&&&&&&&this.mockMvc&=&MockMvcBuilders.standaloneSetup(personalDiskService)&&
&&&&&&&&&&&&&&&&.build();&&
&&&&@Test&&
&&&&public&void&searchPersonalFile()&throws&Exception&{&&
&&&&&&&&SearchFileParamDTO&sf&=&new&SearchFileParamDTO();&&
&&&&&&&&sf.setEntId(entId);&&
&&&&&&&&sf.setKey(&li&);&&
&&&&&&&&sf.setUserId(adminId);&&
&&&&&&&&String&json&=&PojoMapper.toJson(sf);&&
&&&&&&&&String&re&=&MockUtil.mock(this.mockMvc,&RESTurl.searchPersonalFile,&&
&&&&&&&&&&&&&&&&json);&&
&&&&&&&&MockUtil.check(re,&SearchPersonalFilesDTO.class,&&searchPersonalFile&);&&
&当然@setup里面是每个@test执行时都会执行一次的,所以有些需要每次都实例化的参数可以放进来
Java代码&&
@Autowired&&
&&&&private&ShareDiskService&shareDiskS&&
&&&&@Before&&
&&&&public&void&setup()&{&&
&&&&&&&&this.mockMvc&=&MockMvcBuilders.standaloneSetup(shareDiskService)&&
&&&&&&&&&&&&&&&&.build();&&
&&&&&&&&try&{&&
&&&&&&&&&&&&initDatas();&&
&&&&&&&&}&catch&(Exception&e)&{&&
&&&&&&&&&&&&e.printStackTrace();&&
&&&&&&&&}&&
&&&&private&void&initDatas()&throws&UnsupportedEncodingException,&Exception&{&&
&&&&&&&&FileIdVersion&=&MockUtil.updateVersion(mockMvc,RESTurl.getShareFileById,FileId,ShareFileDTO.class);&&
&&&&&&&&File2IdVersion&=&MockUtil.updateVersion(mockMvc,RESTurl.getShareFileById,File2Id,ShareFileDTO.class);&&
&&&&&&&&oldPicFolderVersion&=&MockUtil.updateVersion(mockMvc,RESTurl.getShareFolderById,oldPicFolderId,ShareFolderDTO.class);&&
&ok!基本使用大致如此,代码没有些注释,但是很简单,看懂是没问题的。权当抛砖引玉。希望大家加以指正!
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:4760024次
积分:45122
积分:45122
排名:第58名
原创:226篇
转载:2352篇
评论:496条
(2)(74)(67)(96)(177)(114)(86)(40)(43)(73)(15)(10)(17)(12)(6)(20)(27)(54)(71)(97)(74)(32)(2)(24)(21)(62)(60)(36)(23)(27)(46)(34)(76)(63)(121)(142)(74)(54)(120)(77)(42)(4)(12)(19)(1)(9)(15)(19)(18)(16)(31)(79)(68)

我要回帖

更多关于 spring test mockmvc 的文章

 

随机推荐