- 浏览: 2542722 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Spring3 and REST Integeration(VII)Controller JUnit Test and Mock/Spring Test MVC
Spring Test MVC
I found a spring mvc test framework in github.com, that is nice to have this open source project.
The pom.xml will be changed as follow to get dependencies:
<repositories]]>
<!-- http://maven.springframework.org/snapshot -->
<repository]]>
<id]]>springframe_snapshot_repo</id]]>
<name]]>springframe snapshot repo</name]]>
<url]]>http://maven.springframework.org/snapshot</url]]>
</repository]]>
<!-- http://repository.sonatype.org/content/groups/public/ -->
<repository]]>
<id]]>sonatype_repo</id]]>
<name]]>sonatyperepo</name]]>
<url]]>http://repository.sonatype.org/content/groups/public/</url]]>
</repository]]>
<!-- http://developer.k-int.com/maven2/ -->
<repository]]>
<id]]>k_int_repo</id]]>
<name]]>k intrepo</name]]>
<url]]>http://developer.k-int.com/maven2/</url]]>
</repository]]>
<repository]]>
<id]]>maven_repo1</id]]>
<name]]>maven repo1 Repository</name]]>
<url]]>http://search.maven.org</url]]>
</repository]]>
<repository]]>
<id]]>maven_jboss</id]]>
<name]]>mavenjboss repository</name]]>
<url]]>http://repository.jboss.com</url]]>
</repository]]>
<repository]]>
<id]]>maven_java</id]]>
<name]]>maven java repository</name]]>
<url]]>http://download.java.net/maven/2/</url]]>
</repository]]>
<repository]]>
<id]]>sillycat</id]]>
<name]]>sillycat Repository</name]]>
<url]]>https://sillycat.googlecode.com/svn/repository</url]]>
</repository]]>
<repository]]>
<id]]>alfresco-maven-release</id]]>
<name]]>Alfresco Maven Release Repository</name]]>
<url]]>http://maven.alfresco.com/nexus/content/repositories/releases/</url]]>
</repository]]>
</repositories]]>
<pluginRepositories]]>
<pluginRepository]]>
<id]]>springframe_snapshot_repo</id]]>
<name]]>springframe snapshot repo</name]]>
<url]]>http://maven.springframework.org/snapshot</url]]>
</pluginRepository]]>
<pluginRepository]]>
<id]]>sonatype_repo</id]]>
<name]]>sonatyperepo</name]]>
<url]]>http://repository.sonatype.org/content/groups/public/</url]]>
</pluginRepository]]>
<pluginRepository]]>
<id]]>k_int_repo</id]]>
<name]]>k intrepo</name]]>
<url]]>http://developer.k-int.com/maven2/</url]]>
</pluginRepository]]>
<pluginRepository]]>
<id]]>maven_repo1</id]]>
<name]]>maven repo1 Repository</name]]>
<url]]>http://search.maven.org</url]]>
</pluginRepository]]>
<pluginRepository]]>
<id]]>maven_jboss</id]]>
<name]]>mavenjboss repository</name]]>
<url]]>http://repository.jboss.com</url]]>
</pluginRepository]]>
<pluginRepository]]>
<id]]>maven_java</id]]>
<name]]>maven java repository</name]]>
<url]]>http://download.java.net/maven/2/</url]]>
</pluginRepository]]>
<pluginRepository]]>
<id]]>sillycat</id]]>
<name]]>sillycat Repository</name]]>
<url]]>https://sillycat.googlecode.com/svn/repository</url]]>
</pluginRepository]]>
<pluginRepository]]>
<id]]>alfresco-maven-release</id]]>
<name]]>Alfresco Maven Release Repository</name]]>
<url]]>http://maven.alfresco.com/nexus/content/repositories/releases/</url]]>
</pluginRepository]]>
</pluginRepositories]]>
<dependency]]>
<groupId]]>org.springframework</groupId]]>
<artifactId]]>spring-context</artifactId]]>
<version]]>3.1.1.RELEASE</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.springframework</groupId]]>
<artifactId]]>spring-web</artifactId]]>
<version]]>3.1.1.RELEASE</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.springframework</groupId]]>
<artifactId]]>spring-webmvc</artifactId]]>
<version]]>3.1.1.RELEASE</version]]>
</dependency]]>
<dependency]]>
<groupId]]>javax.servlet</groupId]]>
<artifactId]]>servlet-api</artifactId]]>
<version]]>3.0-alpha-1</version]]>
<scope]]>provided</scope]]>
</dependency]]>
<dependency]]>
<groupId]]>org.codehaus.jackson</groupId]]>
<artifactId]]>jackson-core-asl</artifactId]]>
<version]]>1.9.6</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.codehaus.jackson</groupId]]>
<artifactId]]>jackson-mapper-asl</artifactId]]>
<version]]>1.9.6</version]]>
</dependency]]>
<dependency]]>
<groupId]]>log4j</groupId]]>
<artifactId]]>log4j</artifactId]]>
<version]]>1.2.15</version]]>
</dependency]]>
<dependency]]>
<groupId]]>junit</groupId]]>
<artifactId]]>junit</artifactId]]>
<version]]>4.10</version]]>
<scope]]>test</scope]]>
</dependency]]>
<dependency]]>
<groupId]]>org.springframework</groupId]]>
<artifactId]]>spring-test</artifactId]]>
<version]]>3.1.1.RELEASE</version]]>
<scope]]>test</scope]]>
</dependency]]>
<dependency]]>
<groupId]]>org.springframework</groupId]]>
<artifactId]]>spring-mock</artifactId]]>
<version]]>2.0.8</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.mockito</groupId]]>
<artifactId]]>mockito-core</artifactId]]>
<version]]>1.9.0</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.springframework</groupId]]>
<artifactId]]>spring-test-mvc</artifactId]]>
<version]]>1.0.0.BUILD-SNAPSHOT</version]]>
</dependency]]>
<dependency]]>
<groupId]]>com.jayway.jsonpath</groupId]]>
<artifactId]]>json-path</artifactId]]>
<version]]>0.5.6</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.hamcrest</groupId]]>
<artifactId]]>hamcrest-core</artifactId]]>
<version]]>1.3.RC2</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.hamcrest</groupId]]>
<artifactId]]>hamcrest-library</artifactId]]>
<version]]>1.3.RC2</version]]>
</dependency]]>
And the test implementation will be as follow:
package com.sillycat.easyrestserver.controller;
import static org.springframework.test.web.server.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.server.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.server.result.MockMvcResultMatchers.status;
import java.io.IOException;
import javax.servlet.ServletException;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.http.MediaType;
import org.springframework.test.web.server.request.MockMvcRequestBuilders;
import org.springframework.test.web.server.setup.MockMvcBuilders;
import com.sillycat.easyrestserver.model.Company;
import com.sillycat.easyrestserver.model.Person;
import com.sillycat.easyrestserver.service.PersonService;
public class PersonControllerTest {
@Mock
private PersonService mockPersonService;
PersonController personController;
Person person;
ObjectMapper jsonMapper;
@Before
public void setUp() throws ServletException, IOException {
MockitoAnnotations.initMocks(this);
personController = new PersonController();
personController.setPersonService(mockPersonService);
person = new Person();
person.setCompany(new Company());
person.setId(3);
person.setPersonName("person3");
jsonMapper = new ObjectMapper();
}
@Test
public void get() throws Exception {
Mockito.when(mockPersonService.get(3)).thenReturn(person);
MockMvcBuilders
.standaloneSetup(personController)
.build()
.perform(
MockMvcRequestBuilders.get("/person/3").accept(
MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().type(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.personName").value("person3"));
}
@Test
public void add() throws Exception {
person.setId(null);
String jsonPerson = jsonMapper.writeValueAsString(person);
MockMvcBuilders
.standaloneSetup(personController)
.build()
.perform(
MockMvcRequestBuilders.post("/person")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(jsonPerson.getBytes()))
.andExpect(status().isOk())
.andExpect(content().type(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.personName").value("person3"));
}
}
references:
Spring Test MVC
I found a spring mvc test framework in github.com, that is nice to have this open source project.
The pom.xml will be changed as follow to get dependencies:
<repositories]]>
<!-- http://maven.springframework.org/snapshot -->
<repository]]>
<id]]>springframe_snapshot_repo</id]]>
<name]]>springframe snapshot repo</name]]>
<url]]>http://maven.springframework.org/snapshot</url]]>
</repository]]>
<!-- http://repository.sonatype.org/content/groups/public/ -->
<repository]]>
<id]]>sonatype_repo</id]]>
<name]]>sonatyperepo</name]]>
<url]]>http://repository.sonatype.org/content/groups/public/</url]]>
</repository]]>
<!-- http://developer.k-int.com/maven2/ -->
<repository]]>
<id]]>k_int_repo</id]]>
<name]]>k intrepo</name]]>
<url]]>http://developer.k-int.com/maven2/</url]]>
</repository]]>
<repository]]>
<id]]>maven_repo1</id]]>
<name]]>maven repo1 Repository</name]]>
<url]]>http://search.maven.org</url]]>
</repository]]>
<repository]]>
<id]]>maven_jboss</id]]>
<name]]>mavenjboss repository</name]]>
<url]]>http://repository.jboss.com</url]]>
</repository]]>
<repository]]>
<id]]>maven_java</id]]>
<name]]>maven java repository</name]]>
<url]]>http://download.java.net/maven/2/</url]]>
</repository]]>
<repository]]>
<id]]>sillycat</id]]>
<name]]>sillycat Repository</name]]>
<url]]>https://sillycat.googlecode.com/svn/repository</url]]>
</repository]]>
<repository]]>
<id]]>alfresco-maven-release</id]]>
<name]]>Alfresco Maven Release Repository</name]]>
<url]]>http://maven.alfresco.com/nexus/content/repositories/releases/</url]]>
</repository]]>
</repositories]]>
<pluginRepositories]]>
<pluginRepository]]>
<id]]>springframe_snapshot_repo</id]]>
<name]]>springframe snapshot repo</name]]>
<url]]>http://maven.springframework.org/snapshot</url]]>
</pluginRepository]]>
<pluginRepository]]>
<id]]>sonatype_repo</id]]>
<name]]>sonatyperepo</name]]>
<url]]>http://repository.sonatype.org/content/groups/public/</url]]>
</pluginRepository]]>
<pluginRepository]]>
<id]]>k_int_repo</id]]>
<name]]>k intrepo</name]]>
<url]]>http://developer.k-int.com/maven2/</url]]>
</pluginRepository]]>
<pluginRepository]]>
<id]]>maven_repo1</id]]>
<name]]>maven repo1 Repository</name]]>
<url]]>http://search.maven.org</url]]>
</pluginRepository]]>
<pluginRepository]]>
<id]]>maven_jboss</id]]>
<name]]>mavenjboss repository</name]]>
<url]]>http://repository.jboss.com</url]]>
</pluginRepository]]>
<pluginRepository]]>
<id]]>maven_java</id]]>
<name]]>maven java repository</name]]>
<url]]>http://download.java.net/maven/2/</url]]>
</pluginRepository]]>
<pluginRepository]]>
<id]]>sillycat</id]]>
<name]]>sillycat Repository</name]]>
<url]]>https://sillycat.googlecode.com/svn/repository</url]]>
</pluginRepository]]>
<pluginRepository]]>
<id]]>alfresco-maven-release</id]]>
<name]]>Alfresco Maven Release Repository</name]]>
<url]]>http://maven.alfresco.com/nexus/content/repositories/releases/</url]]>
</pluginRepository]]>
</pluginRepositories]]>
<dependency]]>
<groupId]]>org.springframework</groupId]]>
<artifactId]]>spring-context</artifactId]]>
<version]]>3.1.1.RELEASE</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.springframework</groupId]]>
<artifactId]]>spring-web</artifactId]]>
<version]]>3.1.1.RELEASE</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.springframework</groupId]]>
<artifactId]]>spring-webmvc</artifactId]]>
<version]]>3.1.1.RELEASE</version]]>
</dependency]]>
<dependency]]>
<groupId]]>javax.servlet</groupId]]>
<artifactId]]>servlet-api</artifactId]]>
<version]]>3.0-alpha-1</version]]>
<scope]]>provided</scope]]>
</dependency]]>
<dependency]]>
<groupId]]>org.codehaus.jackson</groupId]]>
<artifactId]]>jackson-core-asl</artifactId]]>
<version]]>1.9.6</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.codehaus.jackson</groupId]]>
<artifactId]]>jackson-mapper-asl</artifactId]]>
<version]]>1.9.6</version]]>
</dependency]]>
<dependency]]>
<groupId]]>log4j</groupId]]>
<artifactId]]>log4j</artifactId]]>
<version]]>1.2.15</version]]>
</dependency]]>
<dependency]]>
<groupId]]>junit</groupId]]>
<artifactId]]>junit</artifactId]]>
<version]]>4.10</version]]>
<scope]]>test</scope]]>
</dependency]]>
<dependency]]>
<groupId]]>org.springframework</groupId]]>
<artifactId]]>spring-test</artifactId]]>
<version]]>3.1.1.RELEASE</version]]>
<scope]]>test</scope]]>
</dependency]]>
<dependency]]>
<groupId]]>org.springframework</groupId]]>
<artifactId]]>spring-mock</artifactId]]>
<version]]>2.0.8</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.mockito</groupId]]>
<artifactId]]>mockito-core</artifactId]]>
<version]]>1.9.0</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.springframework</groupId]]>
<artifactId]]>spring-test-mvc</artifactId]]>
<version]]>1.0.0.BUILD-SNAPSHOT</version]]>
</dependency]]>
<dependency]]>
<groupId]]>com.jayway.jsonpath</groupId]]>
<artifactId]]>json-path</artifactId]]>
<version]]>0.5.6</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.hamcrest</groupId]]>
<artifactId]]>hamcrest-core</artifactId]]>
<version]]>1.3.RC2</version]]>
</dependency]]>
<dependency]]>
<groupId]]>org.hamcrest</groupId]]>
<artifactId]]>hamcrest-library</artifactId]]>
<version]]>1.3.RC2</version]]>
</dependency]]>
And the test implementation will be as follow:
package com.sillycat.easyrestserver.controller;
import static org.springframework.test.web.server.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.server.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.server.result.MockMvcResultMatchers.status;
import java.io.IOException;
import javax.servlet.ServletException;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.http.MediaType;
import org.springframework.test.web.server.request.MockMvcRequestBuilders;
import org.springframework.test.web.server.setup.MockMvcBuilders;
import com.sillycat.easyrestserver.model.Company;
import com.sillycat.easyrestserver.model.Person;
import com.sillycat.easyrestserver.service.PersonService;
public class PersonControllerTest {
@Mock
private PersonService mockPersonService;
PersonController personController;
Person person;
ObjectMapper jsonMapper;
@Before
public void setUp() throws ServletException, IOException {
MockitoAnnotations.initMocks(this);
personController = new PersonController();
personController.setPersonService(mockPersonService);
person = new Person();
person.setCompany(new Company());
person.setId(3);
person.setPersonName("person3");
jsonMapper = new ObjectMapper();
}
@Test
public void get() throws Exception {
Mockito.when(mockPersonService.get(3)).thenReturn(person);
MockMvcBuilders
.standaloneSetup(personController)
.build()
.perform(
MockMvcRequestBuilders.get("/person/3").accept(
MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().type(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.personName").value("person3"));
}
@Test
public void add() throws Exception {
person.setId(null);
String jsonPerson = jsonMapper.writeValueAsString(person);
MockMvcBuilders
.standaloneSetup(personController)
.build()
.perform(
MockMvcRequestBuilders.post("/person")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.body(jsonPerson.getBytes()))
.andExpect(status().isOk())
.andExpect(content().type(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.personName").value("person3"));
}
}
references:
发表评论
-
RESTful JSON Mock Server
2015-03-19 11:58 792RESTful JSON Mock Server C ... -
Performance Tool(7)Improve Lua and Wrk
2015-01-17 06:37 1032Performance Tool(7)Improve Lua ... -
Performance Tool(6)Gatling Upgrade to 2.1.2 Version Or wrk
2015-01-10 01:15 974Performance Tool(6)Gatling Upg ... -
Performance Tool(5)Upgrade to 2.0.x
2014-08-27 03:34 1124Performance Tool(5)Upgrade to 2 ... -
Performance Tool(4)CSV File Data Feeder
2014-08-25 10:50 1024Performance Tool(4)CSV File Dat ... -
wrk with LuaJIT
2014-08-19 06:30 1327wrk with LuaJITHere is an exa ... -
Performance Tool(3)Gatling Upgrade and Cluster
2014-07-25 02:32 1327Performance Tool(3)Gatling Upgr ... -
WRK a HTTP Benchmarking Tool
2014-03-07 04:42 1139WRK a HTTP Benchmarking Tool1 ... -
Performance Tool(1)Gatling
2013-03-15 05:28 1290Performance Tool(1)Gatling 1. ... -
Jenkins Configuration(4)Improve Shell Script Debug/Info Message
2013-01-07 06:32 1334Jenkins Configuration(4)Improve ... -
Jenkins Configuration(3)Shell Script
2012-12-28 01:17 2674Jenkins Configuration(3)Shell S ... -
Eclipse Plugin(2)SOAP UI
2012-06-08 10:48 1346Eclipse Plugin(2)SOAP UI Plugi ... -
Spring3 and REST Integration(VI)Controller JUnit Test and Mock/Spring HandlerAda
2012-04-06 15:51 1816Spring3 and REST Integration(VI ... -
Spring3 and REST Integration(V)Controller JUnit Test and Mock/HandlerAdapter
2012-04-06 15:41 2836Spring3 and REST Integration(V) ... -
Spring3 and REST Integration(IV)Controller JUnit Test and Mock/Servlet
2012-04-06 15:13 1976Spring3 and REST Integration(IV ... -
Jbehave(2)Some Improvement and POM changes
2012-03-28 23:11 1424Jbehave(2)Some Improvement and ... -
buildr(1)Introduce and Install
2011-12-23 16:37 2180buildr(1)Introduce and Install ... -
Jbehave(1) First Web Page Sample
2011-10-26 15:00 2200Jbehave(1) First Web Page Sampl ... -
WarcraftIII Problem on English Win7
2011-07-25 10:18 1939WarcraftIII Problem on English ... -
Web Performance Test Tool
2011-05-10 15:37 1449Web Performance Test Tool 1. F ...
相关推荐
### Pro Spring for Integration #### 一、概览与背景 《Pro Spring for Integration》是一本由著名出版社Apress出版的英文书籍,旨在介绍如何利用Spring Integration框架构建企业级的集成解决方案。Spring作为一...
而这些组件被分别整合在核心容器(Core Container)、AOP(Aspect Oriented Programming)和设备支持(Instrmentation)、数据访问与集成(Data Access/Integeration)、Web、消息(Messaging)和Test等6个模块中。
在WebSphere Integration Development中,可以使用Business Process Model and Notation (BPMN) 图形标准来描绘这些流程。 然后,我们需要创建人工任务节点。这些任务可以是简单的信息输入,也可以是复杂的决策过程...
docker-jenkins-integeration
标题:Process Integration 7.0标准的安装文档 在IT领域,尤其是企业级软件的部署与维护中,标准安装文档扮演着至关重要的角色。本文档是关于SAP NetWeaver Process Integration (PI) 7.1版本在Windows操作系统上...
三阶方程y=ax^3+bx^2+cx+d积分则得到y=ax^4/4+bx^3/3+cx^2/2+dx+m,其中m均为积分常数。 在SCL编程中实现积分算法,通常需要将X轴分成多个小段(长条),每一段近似为梯形,然后计算每个梯形的面积并累加。梯形面积...
3. **Steps**:Kettle提供了丰富的预定义步骤,涵盖数据输入、输出、转换等各个方面。用户也可以自定义步骤以满足特定需求。例如,`Table Input`用于从数据库中读取数据,`CSV File Output`则用于写入CSV文件。 4. ...
利用碎片化的时间学习即可,每天学习一小时或3个课时即可达到效果;时间充足者,自由安排 课程目标: 促使教育公平,打破教育壁垒,让ETL更加的大众化使用 课程简介: 【可视化介绍】 Kettle 是 PDI 以前的名称...
2. 新建一个Integeration Service项目 3. 在该项目中,右键单击SSIS包,然后选择“新建SSIS包” 10.2 商务智能中的数据挖掘模型设计 数据挖掘模型设计是指根据商务智能应用的需求,设计和建立数据挖掘模型,以便...
bootstrap-easy-sidebar, 围绕主题集成和灵活性设计的Bootstrap 工具条 bootstrap-easy-sidebar我看到了 Bootstrap的几个边栏,找到了大多数缺乏主题integeration和灵活性的领域。 通过继承导航栏的所有样式,然后在...
工业 4.0 单元 (i4C) - 根据工业 4.0 概念的机器人单元该项目的第一阶段涉及工业 4.0 背景下的机器人单元设计,并在布尔诺理工大学机械工程学院自动化与计算机科学研究所实施。 这一阶段项目的主要部分是单元的建设...