使用mock测试Controller时报错如下

java.lang.NoClassDefFoundError: javax/servlet/SessionCookieConfig
at org.springframework.test.web.servlet.setup.StandaloneMockMvcBuilder.initWebAppContext(StandaloneMockMvcBuilder.java:311)
at org.springframework.test.web.servlet.setup.AbstractMockMvcBuilder.build(AbstractMockMvcBuilder.java:127)
at com.springinaction.spittr.controller.HomeControllerTest.testHome(HomeControllerTest.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassNotFoundException: javax.servlet.SessionCookieConfig
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 29 more

在 debug 模式下,可以知道 standaloneSetup(clrr) 得到的是 StandaloneMockMvcBuilder 对象和 exception 中报异常的类名一致。

在 Cannot create MockHttpSession with servlet 2.x, spring得到下面有用提示:Spring 4.0 需要得到 servlet 3.0 或以上版本的支持。

The Servlet API mocks in Spring Framework 4.0 support Servlet 3.0 and higher, as can be seen inGitHub commit deba32cad9.

追踪上面的 github 链接,可以发现 SessionCookieConfig 是在升级 servlet API 到 3.0 变动中新增的。

在查询SessionCookieConfig 文档,可以知道 SessionCookieConfig 是在 servlet 3.0 才引入的。

反观自己项目 pom.xml ,用的是 servlet 2.5 ,可以判断得到,是自己项目引用的 servlet 版本过低导致的,需要升级到 3.0 或以上。

mock测试SpringMVC controller报错的更多相关文章

  1. 关于SpringMVC项目报错:java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/xxxx.xml]

    关于SpringMVC项目报错:java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/xxxx ...

  2. 导入MySQL测试数据库employee报错

    导入MySQL测试数据库employee 报错 下载地址:https://launchpad.net/test-db/ 上传解压: [root@001 ~]# tar xf employees_db- ...

  3. Spring-test使用JUnit时,测试类autowired报错,create bean error

    Spring-test使用JUnit时,测试类里面使用autowired会报错, 报create bean error...... 但是controller里面@autowired可以正常运行的. 在 ...

  4. IDEA新建SpringMVC项目报错解决办法

    网页运行的错误: HTTP Status 500 - Handler processing failed; nested exception is java.lang.NoClassDefFoundE ...

  5. python+Django+test 测试数据库生成报错

    前提: 使用Django自带的test进行单元测试. 问题描述: 运行:python manage.py test,报错,出现数据库乱码的现象,报错如下: Creating test database ...

  6. 项目中访问controller报错:HTTP Status 500 - Servlet.init() for servlet spring threw exception

    直接访问controller路径http://localhost:8080/index报错: HTTP Status 500 - Servlet.init() for servlet spring t ...

  7. SpringMVC版本报错解决办法

    报错代码: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...

  8. struts配置测试中遇到报错信息,记录下

    tomcat7 jdk7myeclipse2014 部署完成后,访问页面报错struts.xml文件内容: <?xml version="1.0" encoding=&quo ...

  9. spring-mvc.xml报错cvc-complex-type.2.4.c

    添加 <!-- 定义请求处理映射HandlerMapping --> <bean id = "handlerMapping" class = "org. ...

随机推荐

  1. pycharm如何回到过去某个时间

    在编写代码是,我们可能会写错代码,或者是误删某个文件,那么问题来了,如何回到过去的某个时间段,来弥补我们犯下的错呢? 1.如果是恢复删除的文件则右击之前文件所在的文件夹 2.右击文件夹的显示效果如图 ...

  2. How to implement a custom PropertyEditor so that it supports Appearance rules provided by the ConditionalAppearance module

    https://www.devexpress.com/Support/Center/Question/Details/T505528/how-to-implement-a-custom-propert ...

  3. 浅谈HTTP中GET和POST请求方式的区别

    浅谈HTTP中GET和POST请求的区别 HTTP认知: HTTP是基于TCP/IP的关于数据如何在万维网中如何通信的协议.HTTP的底层是TCP/IP.所以GET和POST的底层也是TCP/IP,也 ...

  4. Python之Django的Model详解

    一.创建数据库 创建数据库 进入数据库: mysql -uroot -p 创建数据库: CREATE DATABASE test1 CHARSET=utf8; 连接数据库 虚拟环境中安装数据库模块:p ...

  5. Python获取每一位的数字,并返回到列表

    通过计算 def calc(value): result = [] while value: result.append(value % 10) value = value // 10 #逆序,按正常 ...

  6. SQL注入原理&分类&危害&防御

    SQL是什么? 结构化查询语句 SQL注入是什么? 是一种将SQL 语句插入或添加到用户输入的参数中,这些参数传递到后台服务器,加以解析并执行 造成注入的原因/原理? 1.对用户输入的参数没有进行严格 ...

  7. Netty源码分析第2章(NioEventLoop)---->第8节: 执行任务队列

      Netty源码分析第二章: NioEventLoop   第八节: 执行任务队列 继续回到NioEventLoop的run()方法: protected void run() { for (;;) ...

  8. Netty源码分析第5章(ByteBuf)---->第6节: 命中缓存的分配

    Netty源码分析第6章: ByteBuf 第六节: 命中缓存的分配 上一小节简单分析了directArena内存分配大概流程, 知道其先命中缓存, 如果命中不到, 则区分配一款连续内存, 这一小节带 ...

  9. Windows下LimeSDR Mini使用说明

    本文内容.开发板及配件仅限用于学校或科研院所开展科研实验! 淘宝店铺名称:开源SDR实验室 LimeSDR链接:https://item.taobao.com/item.htm?spm=a230r.1 ...

  10. IIS 无法加载 CSS,JS的问题

    IIS 能加载 aspx,但不能加载里面的 js,css ,感觉有点坑. 解决方案如下:http://www.pageadmin.net/article/20121001/479.html 原来是没有 ...