分享

当在添加拦截器后,自己的测试类用不了怎么办!!

 新进小设计 2021-05-31

  随笔内容写得可能不是很好,不喜勿喷。

  在我们添加拦截器后,运行测试类居然先报错了,没写之前还能用!!!!崩溃。

  首先我们附上报错信息。

  

`at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0': Invocation of init method failed; nested exception is java.lang.IllegalStateException: WebApplicationObjectSupport instance [ResourceHttpRequestHandler ["static/"]] does not run in a WebApplicationContext but in: org.springframework.context.support.GenericApplicationContext@27c20538, started on Mon Jan 04 13:50:56 CST 2021`

 

  这里代码我们先关注这里。 

`Error creating bean with name 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'`

  这里说我们在创建这个名为“org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0”的bean时出错  这里应该是找不到了,所以我们要加注解  

> > `@WebAppConfiguration("src/main/resources")` 声明这个静态资源的位置 就可以查找了,测试类头部完整代码 ` `@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") @WebAppConfiguration("src/main/resources")`

然后我们运行就可以啦!!

补充:如果还报错,可能就是servlet-api过低,版本不兼容 找到对应版本就好了,我这里用的是这个
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency>

大功告成!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多