当前位置:   article > 正文

关于Junit报java.lang.ClassNotFoundException: javax.servlet.ServletResponse的解决方法_java.lang.noclassdeffounderror: javax/servlet/serv

java.lang.noclassdeffounderror: javax/servlet/servletrequestlistener

在使用Junit对Spring MVC的service层进行测试时,当使用以下两个注解,且配置正确。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/applicationContext.xml")

运行Junit用例时,报如下错误:

  1. java.lang.IllegalStateException: Failed to load ApplicationContext
  2. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
  3. at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
  4. at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
  5. at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
  6. at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)
  7. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)
  8. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)
  9. at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
  10. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)
  11. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)
  12. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
  13. at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
  14. at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
  15. at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
  16. at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
  17. at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
  18. at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
  19. at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
  20. at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
  21. at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
  22. at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
  23. at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
  24. at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
  25. at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
  26. at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
  27. Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0': Failed to introspect bean class [org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler] for lookup method metadata: could not find class that it depends on; nested exception is java.lang.NoClassDefFoundError: javax/servlet/ServletResponse
  28. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:269)
  29. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:1126)
  30. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1099)
  31. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
  32. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
  33. at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312)
  34. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
  35. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308)
  36. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
  37. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
  38. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
  39. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
  40. at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128)
  41. at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
  42. at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:106)
  43. at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:249)
  44. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
  45. at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
  46. ... 24 more
  47. Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletResponse
  48. at java.lang.Class.getDeclaredMethods0(Native Method)
  49. at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
  50. at java.lang.Class.getDeclaredMethods(Class.java:1975)
  51. at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:613)
  52. at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:524)
  53. at org.springframework.util.ReflectionUtils.doWithMethods(ReflectionUtils.java:510)
  54. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:247)
  55. ... 41 more
  56. Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletResponse
  57. at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
  58. at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  59. at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
  60. at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  61. ... 48 more

参考https://blog.csdn.net/babyLetsGo/article/details/90171632,在pom文件中,增加

  1. <dependency>
  2. <groupId>javax.servlet</groupId>
  3. <artifactId>javax.servlet-api</artifactId>
  4. <version>3.0.1</version>
  5. <scope>provided</scope>
  6. </dependency>

问题得到解决,原因是默认使用的servlet-api的版本过低。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/313527
推荐阅读
相关标签
  

闽ICP备14008679号