- 浏览: 2491983 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
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(V)Controller JUnit Test and Mock/HandlerAdapter
HandlerAdapter with Settle Controller
We use handlerAdapter to execute our controller, and use handlerMapping to find our URI related controller class. So that is the problem, we can not mock the service class or manager class in controller.
The base test class is needed as follow:
package com.sillycat.easyrestserver.core;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.BeforeClass;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;
public class ControllerTestBase {
private static HandlerMapping handlerMapping;
private static HandlerAdapter handlerAdapter;
@BeforeClass
public static void setUp() {
if (handlerMapping == null) {
String[] configs = { "file:src/test/resources/test-context.xml" };
XmlWebApplicationContext context = new XmlWebApplicationContext();
context.setConfigLocations(configs);
MockServletContext msc = new MockServletContext();
context.setServletContext(msc);
context.refresh();
msc.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
context);
handlerMapping = (HandlerMapping) context
.getBean(DefaultAnnotationHandlerMapping.class);
handlerAdapter = (HandlerAdapter) context
.getBean(context
.getBeanNamesForType(AnnotationMethodHandlerAdapter.class)[0]);
}
}
public ModelAndView excuteAction(HttpServletRequest request,
HttpServletResponse response) throws Exception {
HandlerExecutionChain chain = handlerMapping.getHandler(request);
//deal with the error message
request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING, true);
final ModelAndView model = handlerAdapter.handle(request, response,
chain.getHandler());
return model;
}
}
The test class will be as follow:
package com.sillycat.easyrestserver.controller;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import com.sillycat.easyrestserver.core.ControllerTestBase;
/**
* handlerAdapter and handlerMapping to verify our controller
* can not mock the service or manager in controller
* @author karl
*
*/
public class PersonControllerTest2 extends ControllerTestBase{
@Test
public void testAdd() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setRequestURI("/person/1");
request.setMethod("GET");
this.excuteAction(request, response);
}
}
But once I run this project, I got the error message as follow:
error message:
java.lang.NullPointerException
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodResolver.useTypeLevelMapping(AnnotationMethodHandlerAdapter.java:675)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodResolver.getCombinedPattern(AnnotationMethodHandlerAdapter.java:690)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodResolver.resolveHandlerMethod(AnnotationMethodHandlerAdapter.java:569)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:431)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
solution:
I go over the spring source code, I really can not understand why the spring framework 3.1.1 exist this kind of problem. But I try this solution:
//deal with the error message
request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING, true);
set the request to true in my base class can solve this problem.
references:
HandlerAdapter with Settle Controller
We use handlerAdapter to execute our controller, and use handlerMapping to find our URI related controller class. So that is the problem, we can not mock the service class or manager class in controller.
The base test class is needed as follow:
package com.sillycat.easyrestserver.core;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.BeforeClass;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.HandlerExecutionChain;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;
public class ControllerTestBase {
private static HandlerMapping handlerMapping;
private static HandlerAdapter handlerAdapter;
@BeforeClass
public static void setUp() {
if (handlerMapping == null) {
String[] configs = { "file:src/test/resources/test-context.xml" };
XmlWebApplicationContext context = new XmlWebApplicationContext();
context.setConfigLocations(configs);
MockServletContext msc = new MockServletContext();
context.setServletContext(msc);
context.refresh();
msc.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
context);
handlerMapping = (HandlerMapping) context
.getBean(DefaultAnnotationHandlerMapping.class);
handlerAdapter = (HandlerAdapter) context
.getBean(context
.getBeanNamesForType(AnnotationMethodHandlerAdapter.class)[0]);
}
}
public ModelAndView excuteAction(HttpServletRequest request,
HttpServletResponse response) throws Exception {
HandlerExecutionChain chain = handlerMapping.getHandler(request);
//deal with the error message
request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING, true);
final ModelAndView model = handlerAdapter.handle(request, response,
chain.getHandler());
return model;
}
}
The test class will be as follow:
package com.sillycat.easyrestserver.controller;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import com.sillycat.easyrestserver.core.ControllerTestBase;
/**
* handlerAdapter and handlerMapping to verify our controller
* can not mock the service or manager in controller
* @author karl
*
*/
public class PersonControllerTest2 extends ControllerTestBase{
@Test
public void testAdd() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setRequestURI("/person/1");
request.setMethod("GET");
this.excuteAction(request, response);
}
}
But once I run this project, I got the error message as follow:
error message:
java.lang.NullPointerException
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodResolver.useTypeLevelMapping(AnnotationMethodHandlerAdapter.java:675)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodResolver.getCombinedPattern(AnnotationMethodHandlerAdapter.java:690)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodResolver.resolveHandlerMethod(AnnotationMethodHandlerAdapter.java:569)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:431)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:424)
solution:
I go over the spring source code, I really can not understand why the spring framework 3.1.1 exist this kind of problem. But I try this solution:
//deal with the error message
request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING, true);
set the request to true in my base class can solve this problem.
references:
发表评论
-
RESTful JSON Mock Server
2015-03-19 11:58 763RESTful JSON Mock Server C ... -
Performance Tool(7)Improve Lua and Wrk
2015-01-17 06:37 1010Performance Tool(7)Improve Lua ... -
Performance Tool(6)Gatling Upgrade to 2.1.2 Version Or wrk
2015-01-10 01:15 941Performance Tool(6)Gatling Upg ... -
Performance Tool(5)Upgrade to 2.0.x
2014-08-27 03:34 1097Performance Tool(5)Upgrade to 2 ... -
Performance Tool(4)CSV File Data Feeder
2014-08-25 10:50 986Performance Tool(4)CSV File Dat ... -
wrk with LuaJIT
2014-08-19 06:30 1300wrk with LuaJITHere is an exa ... -
Performance Tool(3)Gatling Upgrade and Cluster
2014-07-25 02:32 1295Performance Tool(3)Gatling Upgr ... -
WRK a HTTP Benchmarking Tool
2014-03-07 04:42 1104WRK a HTTP Benchmarking Tool1 ... -
Performance Tool(1)Gatling
2013-03-15 05:28 1267Performance Tool(1)Gatling 1. ... -
Jenkins Configuration(4)Improve Shell Script Debug/Info Message
2013-01-07 06:32 1287Jenkins Configuration(4)Improve ... -
Jenkins Configuration(3)Shell Script
2012-12-28 01:17 2628Jenkins Configuration(3)Shell S ... -
Eclipse Plugin(2)SOAP UI
2012-06-08 10:48 1321Eclipse Plugin(2)SOAP UI Plugi ... -
Spring3 and REST Integeration(VII)Controller JUnit Test and Mock/Spring Test MVC
2012-04-06 15:57 1886Spring3 and REST Integeration(V ... -
Spring3 and REST Integration(VI)Controller JUnit Test and Mock/Spring HandlerAda
2012-04-06 15:51 1787Spring3 and REST Integration(VI ... -
Spring3 and REST Integration(IV)Controller JUnit Test and Mock/Servlet
2012-04-06 15:13 1914Spring3 and REST Integration(IV ... -
Jbehave(2)Some Improvement and POM changes
2012-03-28 23:11 1400Jbehave(2)Some Improvement and ... -
buildr(1)Introduce and Install
2011-12-23 16:37 2140buildr(1)Introduce and Install ... -
Jbehave(1) First Web Page Sample
2011-10-26 15:00 2172Jbehave(1) First Web Page Sampl ... -
WarcraftIII Problem on English Win7
2011-07-25 10:18 1907WarcraftIII Problem on English ... -
Web Performance Test Tool
2011-05-10 15:37 1409Web 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....
本资源适合具有一定junit test 的开发者,介绍了mockito最常用的用法
Android下使用JUnitTest用例,可以参见博客:http://www.cnblogs.com/plokmju/p/Android_JUnit.html
分别是两个版本的, 因为spring做单元测试的时候, 是很容易出现版本不兼容的情况, 所以我将我用到的jar包分享出来,zip包中内容:spring_test2.5.5、spring-test-3.2.0.RELEASE 这个我忘了上传有关于junit的jar ,...
spring-Test,Junit4 jar,和测试代码
spring-test-junit5, JUnit ( a )的spring TestContext框架扩展( a ) spring 5测试支持这个项目作为 5的正式 Prototype,在 spring TestContext框架测试支持,并与 SPR-13575结合到 Spring Framework 。 因此,在...
JUnitTest课件
JUnitTest3 and JUnitTest4
junit test
dom4j quick start, xpath 基本用法, junit Test
NULL 博文链接:https://zgdeng.iteye.com/blog/1776487
在java web应用中,通过mock技术可以模拟spring容器,从而进行单元测试,详细可以参见本文
spring framework 3.2.8,包含完整版库文件及api,junit 4库文件及api,还有pdf资料Effective Unit Testing
spring3 + mybatis3 + junit4 可运行项目示例 可进行单元测试,集成测试。。。
automate JUnit tests, monitor code quality, write JUnit tests for the database and web tier refactor legacy code, mock external dependencies using Mockito, and write testable code using test-driven ...
NULL 博文链接:https://zhang-he.iteye.com/blog/1228237
这个是最新的版本,更正了v1.01的一个bug。 JUnit Test Files Maker 是一款批量生成JUnit测试用例文件的软件. (注意:JDK 版本要求 5.0以上,当前版本不支持中文路径和带有空格的路径)
Tests included cover expressions, standard dialect attributes, Spring integration and also most of the example applications, among other topics. Tests under the org.thymeleaf.engine package are ...