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

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

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

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

 package com.catt.base;

 import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
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.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" ,"classpath:spring-servlet.xml"})
@Transactional
@TransactionConfiguration(defaultRollback = true)
/**
* Spring集成Jnuit Action单元测试
* @author dgx
* @data 2014-05-19
*/
public abstract class AbstractActionTestCase {
@Autowired
protected RequestMappingHandlerAdapter handlerAdapter; protected final MockHttpServletRequest request = new MockHttpServletRequest();
protected final MockHttpServletResponse response = new MockHttpServletResponse(); public AbstractActionTestCase()
{
response.setContentType("text/plain;charset=UTF-8");
}
}

测试抽象累

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的更多相关文章

  1. net.sf.json.JSONException: java.lang.NoSuchMethodException

    在尝试将json对象转换为list时候出现了如下错误 Exception in thread "main" net.sf.json.JSONException: java.lang ...

  2. struts2的java.lang.NoSuchMethodException异常处理(转)

    不久前在学习struts时出现这个错误,在网上搜索了半天,发现答案不一.将其总结如下,以方便大家参考. 1. 你有没有试试看 其它的方法能不能用,要是都是这种情况的话,可能是你的Action类没有继承 ...

  3. struts2的java.lang.NoSuchMethodException错误

    不久前在学习struts时出现这个错误,在网上搜索了半天,发现答案不一.将其总结如下,以方便大家参考. 1. 你有没有试试看 其它的方法能不能用,要是都是这种情况的话,可能是你的Action类没有继承 ...

  4. 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 p ...

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

    java.lang.NoSuchMethodException:com.yxq.action.AdminAction.addGoods()   在学习struts2的时有时会出现此异常,现将其总结如下 ...

  6. spring报错 :java.lang.NoSuchMethodException: <init>()

        Spring要求init-method是一个无参数的方法,如果init-method指定的方法中有参数,那么Spring将会抛出java.lang.NoSuchMethodException ...

  7. Java之——java.lang.NoSuchMethodException: [org.springframework.web.multipart.MultipartFile;.()

    转自:https://blog.csdn.net/l1028386804/article/details/65449355 ava.lang.NoSuchMethodException: [org.s ...

  8. struts2的java.lang.NoSuchMethodException异常处理

    1. 你有没有试试看 其它的方法能不能用,要是都是这种情况的话,可能是你的Action类没有继承structs里面的DispatchAction或者其它的类.还有你注意下方法的参数列表,类型顺序要正确 ...

  9. java.lang.NoSuchMethodException: com.hgkj.controler.action.UserAction.newsLoginAction()

    java.lang.NoSuchMethodException: com.hgkj.controler.action.UserAction.newsLoginAction() 不久前在学习struts ...

随机推荐

  1. .NET项目中常用的32个正则表达式总结

    最近没事总结了下最近所用到的正则表达式,下面32个是经常用到的,总结下来与大家分享. . "^-?[1-9]\\d*$",//整数 . "^[1-9]\\d*$" ...

  2. yiled(),wait(),sleep()方法区别

    yiled():让步 wait():等待 sleep():休眠 yiled是让步,会使当前线程由运行状态进入到就绪状态,让其他优先级高线程先执行,但是如果是同一优先级的线程,那么谁先执行就不确定了.它 ...

  3. servletActionContext.getContext默认获取contextmap 数据默认存储在contextmap的request中

  4. 【原创】centos6创建sftp账号,并设置权限和目录

    网上找了个教程,折腾好长时间都不行,最后往死里整,终于弄好了,记录一下. 系统环境:Centos6.9 64bit 完美解决: Permission denied (publickey,gssapi- ...

  5. Virtual Table

    C++对象模型——吴泰 C/C++杂记 C++中的虚函数(表)实现机制以及用C语言对其进行的模拟实现 C++ 多继承和虚继承的内存布局 [已翻译100%] (虚继承参考,推荐) 图说C++对象模型:对 ...

  6. Swift中使用MPMoviePlayerController实现自定义视频播放器界面

    默认情况下播放器自带各种控制按钮,比如前进后退播放暂停等: var url = NSBundle.mainBundle().URLForResource("1", withExte ...

  7. 【BZOJ4820】【SDOI2017】硬币游戏

    Description Solution 设当前走出了一个不匹配任何字符串的串\(S\). ​ 若在\(S\)后随机增添\(m\)个字符,单看这些字符而言,这\(m\)个字符匹配到每个玩家的字符串的概 ...

  8. Linux内核分析实验二:mykernel实验指导(操作系统是如何工作的)

    计算机是如何工作的?(总结)——三个法宝 存储程序计算机工作模型,计算机系统最最基础性的逻辑结构: 函数调用堆栈,高级语言得以运行的基础,只有机器语言和汇编语言的时候堆栈机制对于计算机来说并不那么重要 ...

  9. 如何在 Android 程序中禁止屏幕旋转和重启Activity

    禁止屏幕随手机旋转变化 有时候我们希望让一个程序的界面始终保持在一个方向,不随手机方向旋转而变化:在AndroidManifest.xml的每一个需要禁止转向的Activity配置中加入android ...

  10. bzoj2441【中山市选】小W的问题

    题目描述 有一天,小W找了一个笛卡尔坐标系,并在上面选取了N个整点.他发现通过这些整点能够画出很多个“W”出来.具体来说,对于五个不同的点(x1, y1), (x2, y2), (x3, y3), ( ...