- 浏览: 2487485 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
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 HandlerAda
- 博客分类:
- Testing
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 759RESTful JSON Mock Server C ... -
Performance Tool(7)Improve Lua and Wrk
2015-01-17 06:37 1008Performance Tool(7)Improve Lua ... -
Performance Tool(6)Gatling Upgrade to 2.1.2 Version Or wrk
2015-01-10 01:15 938Performance Tool(6)Gatling Upg ... -
Performance Tool(5)Upgrade to 2.0.x
2014-08-27 03:34 1095Performance Tool(5)Upgrade to 2 ... -
Performance Tool(4)CSV File Data Feeder
2014-08-25 10:50 985Performance Tool(4)CSV File Dat ... -
wrk with LuaJIT
2014-08-19 06:30 1298wrk with LuaJITHere is an exa ... -
Performance Tool(3)Gatling Upgrade and Cluster
2014-07-25 02:32 1291Performance Tool(3)Gatling Upgr ... -
WRK a HTTP Benchmarking Tool
2014-03-07 04:42 1101WRK a HTTP Benchmarking Tool1 ... -
Performance Tool(1)Gatling
2013-03-15 05:28 1265Performance Tool(1)Gatling 1. ... -
Jenkins Configuration(4)Improve Shell Script Debug/Info Message
2013-01-07 06:32 1285Jenkins Configuration(4)Improve ... -
Jenkins Configuration(3)Shell Script
2012-12-28 01:17 2626Jenkins Configuration(3)Shell S ... -
Eclipse Plugin(2)SOAP UI
2012-06-08 10:48 1319Eclipse Plugin(2)SOAP UI Plugi ... -
Spring3 and REST Integeration(VII)Controller JUnit Test and Mock/Spring Test MVC
2012-04-06 15:57 1884Spring3 and REST Integeration(V ... -
Spring3 and REST Integration(V)Controller JUnit Test and Mock/HandlerAdapter
2012-04-06 15:41 2806Spring3 and REST Integration(V) ... -
Spring3 and REST Integration(IV)Controller JUnit Test and Mock/Servlet
2012-04-06 15:13 1908Spring3 and REST Integration(IV ... -
Jbehave(2)Some Improvement and POM changes
2012-03-28 23:11 1399Jbehave(2)Some Improvement and ... -
buildr(1)Introduce and Install
2011-12-23 16:37 2133buildr(1)Introduce and Install ... -
Jbehave(1) First Web Page Sample
2011-10-26 15:00 2167Jbehave(1) First Web Page Sampl ... -
WarcraftIII Problem on English Win7
2011-07-25 10:18 1905WarcraftIII Problem on English ... -
Web Performance Test Tool
2011-05-10 15:37 1401Web Performance Test Tool 1. F ...
相关推荐
struts-junit spring-mock spring-test junit等的javadoc.jar格式的API文档,直接导入Eclipse/MyEclipse/Netbeans等IDE即可实现快速API查询。 包含以下文件: File name -------------------------------------- ...
NULL 博文链接:https://zkf60553.iteye.com/blog/1604723
分别是两个版本的, 因为spring做单元测试的时候, 是很容易出现版本不兼容的情况, 所以我将我用到的jar包分享出来,zip包中内容:hamcrest-core-1.3、junit4.4、junit-4.12、spring_test2.5.5、spring-test-3.2.0....
spring-test-junit5, JUnit ( a )的spring TestContext框架扩展( a ) spring 5测试支持这个项目作为 5的正式 Prototype,在 spring TestContext框架测试支持,并与 SPR-13575结合到 Spring Framework 。 因此,在...
spring-Test,Junit4 jar,和测试代码
分别是两个版本的, 因为spring做单元测试的时候, 是很容易出现版本不兼容的情况, 所以我将我用到的jar包分享出来,zip包中内容:spring_test2.5.5、spring-test-3.2.0.RELEASE 这个我忘了上传有关于junit的jar ,...
本资源适合具有一定junit test 的开发者,介绍了mockito最常用的用法
Android下使用JUnitTest用例,可以参见博客:http://www.cnblogs.com/plokmju/p/Android_JUnit.html
JUnitTest课件
JUnitTest3 and JUnitTest4
NULL 博文链接:https://zgdeng.iteye.com/blog/1776487
junit test
在java web应用中,通过mock技术可以模拟spring容器,从而进行单元测试,详细可以参见本文
spring3 + mybatis3 + junit4 可运行项目示例 可进行单元测试,集成测试。。。
dom4j quick start, xpath 基本用法, junit Test
此资源包含了junit5的jar包和源码,junit5-r5.4.0.zip 和demo的zip文件,对于测试来说是比较有用的
已包含,基本上的项目都可适用了。 junit-4.4.jar spring-test.jar 等
spring framework 3.2.8,包含完整版库文件及api,junit 4库文件及api,还有pdf资料Effective Unit Testing
* Spring’s unit and integration testing support (on JUnit 3.8, JUnit 4, and TestNG). * How to secure applications using Spring Security. Who this book is for This book is for Java developers who ...
Spring Boot 2 REST API的JUnit 5测试用例如何使用Spring Boot Test框架和JUnit 5对RESTful Web服务进行单元测试的示例。它使用MockMvc来提供Spring MVC基础结构,而无需启动HTTP Server。 SpringExtension和@...