Spring3.2 Contorller单元测试参数问题: java.lang.NoSuchMethodException

使用3.2做单元测试的时候发现这个问题,因为之前都是用3.0中的配置适配器使用AnnotationMethodHandlerAdapter,到3.2中升级为RequestMappingHandlerAdapter;

运行之前的单元测试发现报异常:java.lang.NoSuchMethodException,看了一下堆栈发现反射调用的方法签名明显不对,于是看了一下调用的代码,

之前是:handlerAdapter.handle(request, response,  new
HandlerMethod(controller, "setClassBreaksInfo"))

感觉是这里的问,继续看handle的参数,发现还有一个Class<?>...
parameterTypes,预计根据这里还匹配调用参数签名的,于是改为跟调用方法签名对应的ClassType,然后可以正确运行了,整体配置如下:


 1 package com.catt.base;
2
3 import org.junit.runner.RunWith;
4 import org.springframework.beans.factory.annotation.Autowired;
5 import org.springframework.mock.web.MockHttpServletRequest;
6 import org.springframework.mock.web.MockHttpServletResponse;
7 import org.springframework.test.context.ContextConfiguration;
8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
9 import org.springframework.test.context.transaction.TransactionConfiguration;
10 import org.springframework.transaction.annotation.Transactional;
11 import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
12
13
14 @RunWith(SpringJUnit4ClassRunner.class)
15 @ContextConfiguration(locations = { "classpath:applicationContext.xml" ,"classpath:spring-servlet.xml"})
16 @Transactional
17 @TransactionConfiguration(defaultRollback = true)
18 /**
19 * Spring集成Jnuit Action单元测试
20 * @author dgx
21 * @data 2014-05-19
22 */
23 public abstract class AbstractActionTestCase {
24 @Autowired
25 protected RequestMappingHandlerAdapter handlerAdapter;
26
27 protected final MockHttpServletRequest request = new MockHttpServletRequest();
28 protected final MockHttpServletResponse response = new MockHttpServletResponse();
29
30
31 public AbstractActionTestCase()
32 {
33 response.setContentType("text/plain;charset=UTF-8");
34 }
35 }

测试抽象累

package com.catt.action;

import static org.junit.Assert.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;

import com.catt.action.pub.GisAction;
import com.catt.base.AbstractActionTestCase;

public class GisTest extends AbstractActionTestCase{
@Autowired
private GisAction controller;

@Test
public void testSetClassBreaksInfo()
{

request.setRequestURI("/gisAction/setClassBreaksInfo.do");
//request.setMethod(HttpMethod.POST.name());

request.addParameter("userid", "5802919");
request.addParameter("gisSqlId", "1");
request.addParameter("classBreaksType", "2");
request.addParameter("breaksInfo", "1|0|1|0xFF0000|高倒流栅格1,2|2|3|0xFF0000|高倒流栅格2");

HttpSession session = request.getSession();
//设置 认证信息
session.setAttribute("1", 1);

ModelAndView mv = null;

try {
mv = handlerAdapter.handle(request, response, new HandlerMethod(controller, "setClassBreaksInfo",
String.class,String.class,String.class,String.class,HttpServletResponse.class));
} catch (Exception e) {
e.printStackTrace();
}

assertEquals("/setClassBreaksInfo", mv.getViewName());
}
}

单元测试

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

<context:annotation-config />
<context:component-scan base-package="com.catt.action" />

<bean id="handlerAdapter" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="webBindingInitializer">
<bean class="util.web.springmvc.BindingInitializer"/>
</property>
</bean>

<bean class="util.web.springmvc.ExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="java.lang.Exception">/error/500</prop>
</props>
</property>
</bean>

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="util.web.interceptor.AdminLoginInterceptor">
<property name="loginUrl" value="/login.jsp"/>
<property name="excludeUrls">
<list>
<value>/safeMgr/login.do</value>
<value>/safeMgr/sendRedirectLogin.do</value>
<value>/gisAction/getRenderKpi.do</value>
<value>/gisAction/createDataSource.do</value>
<value>/gisAction/getClassBreaksInfo.do</value>
<!-- 以上GIS和登陆配置是必须的 -->
</list>
</property>
</bean>
</mvc:interceptor>
</mvc:interceptors>

<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="utf-8" />

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>

<bean id="SpringBeanUtil" class="util.SpringBeanUtil" />

<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean
class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

</beans>

配置文件

Spring3.2 Contorller单元测试参数问题:
java.lang.NoSuchMethodException

时间: 2024-10-11 10:01:28

Spring3.2 Contorller单元测试参数问题: java.lang.NoSuchMethodException的相关文章

MyBatis3.4.0以上的分页插件错误:Could not find method on interface org.apache.ibatis.executor.statement.StatementHandler named prepare. Cause: java.lang.NoSuchMethodException: org.apache.ibatis.executor.stateme

错误: Could not find method on interface org.apache.ibatis.executor.statement.StatementHandler named prepare. Cause: java.lang.NoSuchMethodException: org.apache.ibatis.executor.statement.StatementHandler.prepare(java.sql.Connection)] with root cause 问题

java.lang.NoSuchMethodException:com.yxq.action.AdminAction.addGoods()《转载》

java.lang.NoSuchMethodException:com.yxq.action.AdminAction.addGoods()   在学习struts2的时有时会出现此异常,现将其总结如下,方便大家参考: 1. 先尝试下看其它的方法能不能用,要是都是这种情况的话,可能是你的Action类没有继承structs里面的DispatchAction或者其它的类.还有你注意下方法的参数列表,类型顺序要正确才行,是按照以下顺序的:ActionMapping mapping,ActionForm

struts2的java.lang.NoSuchMethodException错误

不久前在学习struts时出现这个错误,在网上搜索了半天,发现答案不一.将其总结如下,以方便大家参考. 1. 你有没有试试看 其它的方法能不能用,要是都是这种情况的话,可能是你的Action类没有继承structs里面的DispatchAction或者其它的类.还有你注意下方法的参数列表,类型顺序要正确才行,是按照以下顺序的:ActionMapping mapping,ActionForm form ,HttpServletRequest request,HttpServletResponse 

java.lang.NoSuchMethodException: [class android.view.View]

05-24 11:38:35.884: E/AndroidRuntime(1819): FATAL EXCEPTION: main05-24 11:38:35.884: E/AndroidRuntime(1819): Process: com.example.activitytest, PID: 181905-24 11:38:35.884: E/AndroidRuntime(1819): java.lang.IllegalStateException: Could not find a met

java.lang.NoSuchMethodException 错误

报错: Stacktraces java.lang.NoSuchMethodException: com.gssw.action.ProAction.update() java.lang.Class.getMethod(Class.java:1607) org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.getActionMethod(AnnotationValidationInterceptor.j

java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addFilter

错误处理:java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addFilter 解决办法: 在tomacat的配置文件context.xml里加上<Loader delegate="true"/>

java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addServlet的解决方案

tomcat7启动后出现:java.lang.NoSuchMethodException: org.apache.catalina.deploy.WebXml addServlet这个问题. 并且访问部署的工程出现404错误. 解决方案1: 在tomacat的context.xml里加上<Loader delegate="true" />. 我是使用方法一解决问题的. 解决方案2(网友提供): 删除您添加在Referenced Libraries 下的catalina.ja

java.lang.NoSuchMethodException解决方法

java.lang.NoSuchMethodException: [Lcn.edu.bnu.land.model.Zbcrxx;.<init>()

JSON中 net.sf.json.JSONException: java.lang.NoSuchMethodException异常

在json对象和java对象转换时 String s = "{'name':'name1','pwd':'pwd1'}"; Person p = (Person)JSONObject.toBean(JSONObject.fromObject(s), Person.class); System.out.println(p.getPwd()); 上面代码中出现以下异常: net.sf.json.JSONException: java.lang.NoSuchMethodException [