spring boot在运行测试类Error creating bean with name 'serverEndpointExporter' defined...问题解决方案

在spring boot单元测试的时候会遇到很多问题,我在使用websocket的时候会运行测试类,报错: Error creating bean with name 'serverEndpointExporter' defined in class path resource [com/Jacklin/config/WebSocketConfig.class] ,我这里引入了注解@ServerEndPoint:

spring boot在运行测试类Error creating bean with name 'serverEndpointExporter' defined...问题解决方案

解决的方式有两种:

第一种方式:去掉测试类的@RunWith(SpringRunner.class),去掉即可,但是这种方式会有局限,比如下方你要@Authwired一个类的时候会报错,我这里不可以,根据你的代码情况。

第二种方式:在SpringBootTest后加上:webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,意思是创建Web应用程序上下文(基于响应或基于servlet),原因:websocket是需要依赖tomcat等容器的启动。所以在测试过程中我们要真正的启动一个tomcat作为容器。

spring boot在运行测试类Error creating bean with name 'serverEndpointExporter' defined...问题解决方案

添加后运行,没有再报错了!!