美文网首页
spring boot 运行测试类时:Error creatin

spring boot 运行测试类时:Error creatin

作者: 平面小狮子 | 来源:发表于2019-06-14 09:39 被阅读0次

问题:spring boot 运行测试类时:Error creating bean with name 'serverEndpointExporter' 。

错误原因猜想:之前打包运行一直好好的,看到serverEndpointExporter立即想到了肯定是引入了webscoket,导致打包无法通过测试,随机开始找解决办法。

@ServerEndpoint 注解在websocket配置类中进行了使用,如下:

websocket配置类

两种解决方式:

第一种:将@RunWith(SpringRunner.class) 去掉即可,但是这种方式会有局限,如果要注入某个service或者dao的时候,使用@Atuowried或者@Resource时 会报错。经常使用测试类做测试的话不建议使用此方法;

去掉测试类的配置

第二种:在SpringBootTest后加上

(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 也可解决。

原因:websocket是需要依赖tomcat等容器的启动。所以在测试过程中我们要真正的启动一个tomcat作为容器。

修改@SpringBootTest

相关文章

网友评论

      本文标题:spring boot 运行测试类时:Error creatin

      本文链接:https://www.haomeiwen.com/subject/kzayfctx.html