- 浏览: 2551554 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
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 801RESTful JSON Mock Server C ... -
Performance Tool(7)Improve Lua and Wrk
2015-01-17 06:37 1037Performance Tool(7)Improve Lua ... -
Performance Tool(6)Gatling Upgrade to 2.1.2 Version Or wrk
2015-01-10 01:15 979Performance Tool(6)Gatling Upg ... -
Performance Tool(5)Upgrade to 2.0.x
2014-08-27 03:34 1128Performance Tool(5)Upgrade to 2 ... -
Performance Tool(4)CSV File Data Feeder
2014-08-25 10:50 1032Performance Tool(4)CSV File Dat ... -
wrk with LuaJIT
2014-08-19 06:30 1335wrk with LuaJITHere is an exa ... -
Performance Tool(3)Gatling Upgrade and Cluster
2014-07-25 02:32 1336Performance Tool(3)Gatling Upgr ... -
WRK a HTTP Benchmarking Tool
2014-03-07 04:42 1144WRK a HTTP Benchmarking Tool1 ... -
Performance Tool(1)Gatling
2013-03-15 05:28 1297Performance Tool(1)Gatling 1. ... -
Jenkins Configuration(4)Improve Shell Script Debug/Info Message
2013-01-07 06:32 1338Jenkins Configuration(4)Improve ... -
Jenkins Configuration(3)Shell Script
2012-12-28 01:17 2682Jenkins Configuration(3)Shell S ... -
Eclipse Plugin(2)SOAP UI
2012-06-08 10:48 1352Eclipse Plugin(2)SOAP UI Plugi ... -
Spring3 and REST Integeration(VII)Controller JUnit Test and Mock/Spring Test MVC
2012-04-06 15:57 1912Spring3 and REST Integeration(V ... -
Spring3 and REST Integration(VI)Controller JUnit Test and Mock/Spring HandlerAda
2012-04-06 15:51 1822Spring3 and REST Integration(VI ... -
Spring3 and REST Integration(IV)Controller JUnit Test and Mock/Servlet
2012-04-06 15:13 1992Spring3 and REST Integration(IV ... -
Jbehave(2)Some Improvement and POM changes
2012-03-28 23:11 1429Jbehave(2)Some Improvement and ... -
buildr(1)Introduce and Install
2011-12-23 16:37 2189buildr(1)Introduce and Install ... -
Jbehave(1) First Web Page Sample
2011-10-26 15:00 2206Jbehave(1) First Web Page Sampl ... -
WarcraftIII Problem on English Win7
2011-07-25 10:18 1947WarcraftIII Problem on English ... -
Web Performance Test Tool
2011-05-10 15:37 1459Web Performance Test Tool 1. F ...
相关推荐
1. **配置测试环境**:引入Spring Test和JUnit相关的依赖,创建一个继承自`AbstractJUnit4SpringContextTests`或`SpringRunner`的测试类。在测试类上使用`@RunWith(SpringRunner.class)`注解启用Spring测试支持,并...
struts-junit spring-mock spring-test junit等的javadoc.jar格式的API文档,直接导入Eclipse/MyEclipse/Netbeans等IDE即可实现快速API查询。 包含以下文件: File name -------------------------------------- ...
3. **设置JUnitTest** 首先,在Android项目中创建一个测试目录,通常位于`app/src/androidTest/java`。然后,在这个目录下创建一个新的Java类,继承自`androidx.test.ext.junit.runners.AndroidJUnit4`。这样,你的...
标题“spring2 junit3”指的是在Spring框架的第二个主要版本中使用JUnit3进行单元测试的相关内容。这篇博文可能探讨了如何在Spring 2.x时代利用JUnit3进行测试驱动开发(TDD),因为JUnit3是当时广泛使用的Java单元...
接下来,我们将深入探讨Spring Test与JUnit4的结合使用以及如何通过它们进行测试代码的编写。 首先,Spring Test模块提供了一组测试注解,如`@ContextConfiguration`、`@RunWith(SpringRunner.class)`等,这些注解...
分别是两个版本的, 因为spring做单元测试的时候, 是很容易出现版本不兼容的情况, 所以我将我用到的jar包分享出来,zip包中内容:hamcrest-core-1.3、junit4.4、junit-4.12、spring_test2.5.5、spring-test-3.2.0....
《深入理解JUnitTest》 JUnitTest是Java编程领域中广泛使用的单元测试框架,它为开发者提供了方便、快捷的方式来编写和执行测试用例,确保代码的质量和功能的正确性。在这个主题中,我们将深入探讨JUnitTest的基本...
在Java开发过程中,单元测试是确保代码质量的重要环节,而JUnit是Java领域广泛使用的单元测试框架。Mockito则是一个强大的模拟框架,它允许我们在测试中创建和配置模拟对象,以便隔离被测试代码并专注于测试单个行为...
分别是两个版本的, 因为spring做单元测试的时候, 是很容易出现版本不兼容的情况, 所以我将我用到的jar包分享出来,zip包中内容:spring_test2.5.5、spring-test-3.2.0.RELEASE 这个我忘了上传有关于junit的jar ,...
在SSM环境中,使用Junit4和spring-test库进行单元测试是标准做法。下面将详细解释如何使用这两个库以及所需的jar包。 Junit4是Java领域广泛使用的单元测试框架,它提供了一套丰富的注解,使得编写测试用例变得更加...
spring-test-junit5, JUnit ( a )的spring TestContext框架扩展( a ) spring 5测试支持这个项目作为 5的正式 Prototype,在 spring TestContext框架测试支持,并与 SPR-13575结合到 Spring Framework 。 因此,在...
JUnit5引入了新的注解,如@Test、@BeforeEach、@AfterEach等,使得测试更加灵活和可定制。此外,JUnit5还支持并行测试,提高了测试执行效率。 【Mockito使用】 Mockito是一个强大的Mock框架,它可以创建Mock对象并...
《Spring Framework 2.5.6与JUnit 4.4及Spring Test的深度解析》 在软件开发领域,Spring Framework以其强大的依赖注入、面向切面编程(AOP)以及全面的事务管理等功能,成为了Java应用开发的重要基石。而这次我们...
《Spring3与JUnit4深度解析》 在Java开发领域,Spring框架和JUnit测试工具是不可或缺的重要组成部分。Spring3.2.8是Spring框架的一个稳定版本,它提供了丰富的功能,包括依赖注入、AOP(面向切面编程)、MVC(模型-...
JUnit Test Files Maker v1.01 是一个专为开发者设计的工具,用于批量生成JUnit测试用例。这个软件能够显著提高开发效率,特别是在处理大量需要测试的代码时,避免手动编写重复的测试代码。JUnit是Java编程语言中最...
本示例程序是关于"Spring Boot Test"的实践,它展示了如何进行Spring Rest Controller的集成测试和单元测试。 1. **Spring Boot Test模块**: Spring Boot提供了测试支持模块,包含`spring-boot-starter-test`,这...
标题 "junit common test case just for my test" 提示我们这是一组用 JUnit 编写的通用测试用例,专门用于个人测试目的。 JUnit 的核心概念包括测试类、测试方法和断言。测试类通常扩展自 JUnit 的 `TestCase` 类...
这包括JUnit4本身,Spring的`spring-test`模块以及Spring Boot的`spring-boot-starter-test`模块。这三个依赖分别提供了JUnit4的基本功能,Spring对测试的支持以及Spring Boot测试相关的工具和配置。以下是添加依赖...
在本篇文章中,我们将探讨如何利用Spring的MOVE(Model-View-Controller)架构以及JUnit库来执行单元测试。首先,我们需要理解Spring的MOVE架构: 1. **Model**:这是应用的核心业务逻辑部分,它处理数据和业务规则...
在这个“spring3 junit 测试”主题中,我们将深入探讨如何在Spring3环境中集成和使用JUnit进行单元测试。Spring3提供了对JUnit的内置支持,允许我们在测试上下文中注入依赖,模拟服务,以及使用其强大的测试支持类。...