Spring3 and REST Integration(V)Controller JUnit Test and Mock/HandlerAdapter

本文介绍了一种使用 Spring MVC 的 HandlerAdapter 和 HandlerMapping 进行控制器测试的方法,并提供了一个解决 NullPointerException 的方案。通过设置 HttpServletRequest 属性,成功解决了测试过程中出现的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值