加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

构造函数抛出异常;嵌套异常是java.lang.NoClassDefFoundError:j

发布时间:2020-12-15 01:36:54 所属栏目:大数据 来源:网络整理
导读:我正在尝试使用MockMVC设置Junit测试. 从这个link “要么不能使用Servlet API,要么你需要在类路径上提供它”. 我在pom.xml中添加了以下内容但是没有用.任何的想法? 这是跟踪 org.springframework.beans.BeanInstantiationException: Could not instantiate

我正在尝试使用MockMVC设置Junit测试.

从这个link – “要么不能使用Servlet API,要么你需要在类路径上提供它”.

我在pom.xml中添加了以下内容但是没有用.任何的想法?

    

这是跟踪

org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.test.context.web.WebDelegatingSmartContextLoader]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: javax/servlet/ServletContext
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:163)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:105)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:130)
    at org.springframework.test.context.ContextLoaderUtils.resolveContextLoader(ContextLoaderUtils.java:118)
    at org.springframework.test.context.ContextLoaderUtils.buildMergedContextConfiguration(ContextLoaderUtils.java:594)
    at org.springframework.test.context.ContextLoaderUtils.buildMergedContextConfiguration(ContextLoaderUtils.java:560)
    at org.springframework.test.context.TestContext.

好的,根据评论更新,所以我把范围改为编译然后似乎通过了找不到类的问题!

    
最佳答案
这是一个常见问题.正如Aniket Thakur所说,容器将在运行时提供所有Java servlet类.但在测试期间,您需要一个罐子来提供它们.

您添加到pom中的依赖项只是API:它声明了所有内容但不包含任何实现.所以它无济于事.无论如何,你声明它是“提供”,它对maven说“别担心,我知道它将在classpath”.

您必须添加一个依赖项,以便在测试范围内实现所有Java EE类的实现.在我的项目中,我使用glassfish,即使我后来使用tomcat作为servlet容器,但我曾经发现相同问题的依赖性谷歌搜索:


它应该解决你的NoClassDefFoundError问题.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读