记录一次测试类运行的异常:Error creating bean with name 'serverEndpointExporter'

2022-10-02后端1 分钟256 次阅读
AI 摘要鱼粮不足,AI 今天先歇着 · 正文在下面

问题

在运行SpringBoot单元测试时出现异常
image.png
我们往下查看,看到一条具体的错误信息
image.png

shell 复制代码
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverEndpointExporter' defined in class path resource [com/salmon/blogservice/config/WebSocketConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: javax.websocket.server.ServerContainer not available

这个异常大致信息是说ServerEndpointExporter调用初始化方法失败,因为没有一个可用的容器。
image.png
我们追其源码
image.png
image.png
image.png
发现需要依赖tomcat容器运行

解决

在@SpringBootTest注解加上webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT参数即可,意思是启动一个Servlet容器(内置容器),端口为随机端口

java 复制代码
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
觉得有用就点个赞,不用登录也能读

评论

0 条
登录 之后才能评论
—
还没有评论,来说第一句