记录一次测试类运行的异常:Error creating bean with name 'serverEndpointExporter'
AI 摘要鱼粮不足,AI 今天先歇着 · 正文在下面
问题
在运行SpringBoot单元测试时出现异常

我们往下查看,看到一条具体的错误信息

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调用初始化方法失败,因为没有一个可用的容器。

我们追其源码



发现需要依赖tomcat容器运行
解决
在@SpringBootTest注解加上webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT参数即可,意思是启动一个Servlet容器(内置容器),端口为随机端口
java
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
评论
0 条—
还没有评论,来说第一句