- 浏览: 2566435 次
- 性别:
- 来自: 成都
-
文章分类
最新评论
-
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 Integration(VI)Controller JUnit Test and Mock/Spring HandlerAdapter
Spring Annotation and HandlerAdapter
There is no need to use other class, but only the configuration. And I can use mockito to mock our service/manager layer.
The pom.xml changes will be as follow:
<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]]>
The test class implementation will be as follow:
package com.sillycat.easyrestserver.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerMapping;
import com.sillycat.easyrestserver.model.Company;
import com.sillycat.easyrestserver.model.Person;
import com.sillycat.easyrestserver.service.PersonService;
/**
* use mockito to mock our service/manager
* new Controller and use handler to invoke the controller
* @author karl
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:test-context.xml")
public class PersonControllerTest3 {
@Mock
private PersonService mockPersonService;
@Autowired
HandlerAdapter handlerAdapter;
ObjectMapper jsonMapper;
Person person;
PersonController personController;
MockHttpServletRequest mockRequest;
MockHttpServletResponse mockResponse;
@Before
public void setUp() throws ServletException, IOException {
MockitoAnnotations.initMocks(this);
jsonMapper = new ObjectMapper();
person = new Person();
person.setCompany(new Company());
person.setId(3);
person.setPersonName("person3");
personController = new PersonController();
personController.setPersonService(mockPersonService);
mockRequest = new MockHttpServletRequest();
mockRequest.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING,
true);
mockResponse = new MockHttpServletResponse();
}
@Test
public void get() throws Exception {
mockRequest.setMethod("GET");
mockRequest.setRequestURI("/person/3");
mockRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
mockResponse = new MockHttpServletResponse();
Mockito.when(mockPersonService.get(3)).thenReturn(person);
handlerAdapter.handle(mockRequest, mockResponse, personController);
Assert.assertEquals(mockResponse.getStatus(), 200);
Person actualPerson = jsonMapper.readValue(
mockResponse.getContentAsString(), Person.class);
Assert.assertEquals(actualPerson.getId(), person.getId());
}
@Test
public void add() throws Exception {
mockRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
mockRequest.setMethod("POST");
mockRequest.setRequestURI("/person");
person.setId(null);
String jsonPerson = jsonMapper.writeValueAsString(person);
mockRequest.setContent(jsonPerson.getBytes());
handlerAdapter.handle(mockRequest, mockResponse, personController);
Assert.assertEquals(mockResponse.getStatus(), 200);
}
}
references:
Spring Annotation and HandlerAdapter
There is no need to use other class, but only the configuration. And I can use mockito to mock our service/manager layer.
The pom.xml changes will be as follow:
<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]]>
The test class implementation will be as follow:
package com.sillycat.easyrestserver.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerMapping;
import com.sillycat.easyrestserver.model.Company;
import com.sillycat.easyrestserver.model.Person;
import com.sillycat.easyrestserver.service.PersonService;
/**
* use mockito to mock our service/manager
* new Controller and use handler to invoke the controller
* @author karl
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:test-context.xml")
public class PersonControllerTest3 {
@Mock
private PersonService mockPersonService;
@Autowired
HandlerAdapter handlerAdapter;
ObjectMapper jsonMapper;
Person person;
PersonController personController;
MockHttpServletRequest mockRequest;
MockHttpServletResponse mockResponse;
@Before
public void setUp() throws ServletException, IOException {
MockitoAnnotations.initMocks(this);
jsonMapper = new ObjectMapper();
person = new Person();
person.setCompany(new Company());
person.setId(3);
person.setPersonName("person3");
personController = new PersonController();
personController.setPersonService(mockPersonService);
mockRequest = new MockHttpServletRequest();
mockRequest.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING,
true);
mockResponse = new MockHttpServletResponse();
}
@Test
public void get() throws Exception {
mockRequest.setMethod("GET");
mockRequest.setRequestURI("/person/3");
mockRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
mockResponse = new MockHttpServletResponse();
Mockito.when(mockPersonService.get(3)).thenReturn(person);
handlerAdapter.handle(mockRequest, mockResponse, personController);
Assert.assertEquals(mockResponse.getStatus(), 200);
Person actualPerson = jsonMapper.readValue(
mockResponse.getContentAsString(), Person.class);
Assert.assertEquals(actualPerson.getId(), person.getId());
}
@Test
public void add() throws Exception {
mockRequest.setContentType(MediaType.APPLICATION_JSON_VALUE);
mockRequest.setMethod("POST");
mockRequest.setRequestURI("/person");
person.setId(null);
String jsonPerson = jsonMapper.writeValueAsString(person);
mockRequest.setContent(jsonPerson.getBytes());
handlerAdapter.handle(mockRequest, mockResponse, personController);
Assert.assertEquals(mockResponse.getStatus(), 200);
}
}
references:
发表评论
-
RESTful JSON Mock Server
2015-03-19 11:58 806RESTful JSON Mock Server C ... -
Performance Tool(7)Improve Lua and Wrk
2015-01-17 06:37 1046Performance Tool(7)Improve Lua ... -
Performance Tool(6)Gatling Upgrade to 2.1.2 Version Or wrk
2015-01-10 01:15 985Performance Tool(6)Gatling Upg ... -
Performance Tool(5)Upgrade to 2.0.x
2014-08-27 03:34 1139Performance Tool(5)Upgrade to 2 ... -
Performance Tool(4)CSV File Data Feeder
2014-08-25 10:50 1042Performance Tool(4)CSV File Dat ... -
wrk with LuaJIT
2014-08-19 06:30 1350wrk with LuaJITHere is an exa ... -
Performance Tool(3)Gatling Upgrade and Cluster
2014-07-25 02:32 1347Performance Tool(3)Gatling Upgr ... -
WRK a HTTP Benchmarking Tool
2014-03-07 04:42 1156WRK a HTTP Benchmarking Tool1 ... -
Performance Tool(1)Gatling
2013-03-15 05:28 1308Performance Tool(1)Gatling 1. ... -
Jenkins Configuration(4)Improve Shell Script Debug/Info Message
2013-01-07 06:32 1347Jenkins Configuration(4)Improve ... -
Jenkins Configuration(3)Shell Script
2012-12-28 01:17 2699Jenkins Configuration(3)Shell S ... -
Eclipse Plugin(2)SOAP UI
2012-06-08 10:48 1361Eclipse Plugin(2)SOAP UI Plugi ... -
Spring3 and REST Integeration(VII)Controller JUnit Test and Mock/Spring Test MVC
2012-04-06 15:57 1922Spring3 and REST Integeration(V ... -
Spring3 and REST Integration(V)Controller JUnit Test and Mock/HandlerAdapter
2012-04-06 15:41 2848Spring3 and REST Integration(V) ... -
Spring3 and REST Integration(IV)Controller JUnit Test and Mock/Servlet
2012-04-06 15:13 2009Spring3 and REST Integration(IV ... -
Jbehave(2)Some Improvement and POM changes
2012-03-28 23:11 1443Jbehave(2)Some Improvement and ... -
buildr(1)Introduce and Install
2011-12-23 16:37 2202buildr(1)Introduce and Install ... -
Jbehave(1) First Web Page Sample
2011-10-26 15:00 2216Jbehave(1) First Web Page Sampl ... -
WarcraftIII Problem on English Win7
2011-07-25 10:18 1957WarcraftIII Problem on English ... -
Web Performance Test Tool
2011-05-10 15:37 1468Web Performance Test Tool 1. F ...
相关推荐
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
pimpinella_3cd_01_0716
FIB English learning
X86-jq安装包
AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!
大圣挪车小程序1.3.5 前端
Manus.im 产品及开发团队研究报告.pdf
AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!
sun_3ck_01a_0918
下载 1. 单击“立即下载”,以下载该文件。 2. 出现“文件下载”窗口后,单击“保存”,以将文件保存到硬盘。 安装 1. 浏览至文件下载目标位置并双击新下载的文件。 2. 仔细阅读对话窗口中显示的发布信息。 3. 下载并安装对话窗口中标识的任何必备项,然后再继续。 4. 单击“Install”(安装)按钮。 5. 按照其余提示执行更新。 安装 1. 将解压的文件复制到可访问Windows的介质。 2. 将系统重新引导至Windows操作系统。 3. 打开“服务器管理器”->“设备管理器”->“存储控制器”,然后单击“PERC控制器”。 5. 单击“更新驱动程序软件”,并按照提示更新驱动程序。 4. 重新引导系统以使更改生效。
支持所有操作系统一键安装。
matlab程序代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!
AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!
swanson_01_1106
AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!
AB PLC例程代码项目案例 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用!有问题请及时沟通交流。 2、适用人群:计算机相关专业(如计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网、自动化、电子信息等)在校学生、专业老师或者企业员工下载使用。 3、用途:项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 4、如果基础还行,或热爱钻研,亦可在此项目代码基础上进行修改添加,实现其他不同功能。 欢迎下载!欢迎交流学习!不清楚的可以私信问我!
sun_3ck_01_0919
各城市方言距离数据-中山大学岭南学院产业与区域经济研究中心 方言距离是指两种或多种方言之间的相似程度或差异程度。参考中山大学岭南学院产业与区域经济研究中心的刘毓芸等(2015)文献。他们基于方言树图,并参考《汉语方言大词典》和《中国语言地图集》对方言的划分,将汉语方言从宽泛到具体分为以下几个层级:汉语→方言大区→方言区→方言片。为了量化县与县之间的方言差异,他们采用了一种赋值方法: 若它们分属不同方言大区,则距离为3。: 若两个县同属一个方言片,则它们之间的方言距离为0; 若两个县属于同一方言区但不同方言片,则距离为1; 若它们属于同一方言大区但不同方言区,则距离为2; 方言距离是一个反映方言之间相似程度或差异程度的重要指标,它在语音识别、方言研究等领域具有广泛的应用价值。 参考文献:[1]刘毓芸, 徐现祥, 肖泽凯. 2015. 劳动力跨方言流动的倒U型模式[J]. 经济研究, 50(10): 134-146+162. 指标 语系、语族、方言大区、方言区/语支、方言片/语种、Supergroup、Dialect、group、Sub-dialect、groupPref_1、Pref_2、DiaDist、PrefCode_1、PrefCode_2等等。
基于PCA算法的人脸识别MATLAB源码