Spring Boot整合WebSocket时运行测试类会报错
问题
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class DemoApplicationTests {
@Test
void contextLoads() {
System.out.println("整合WebSocket时,运行测试类会报错");
}
}
Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘serverEndpointExporter’ defined in class path resource [com/example/demo/config/WebSocketConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: javax.websocket.server.ServerContainer not available
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘serverEndpointExporter’ defined in class path resource [com/example/demo/config/WebSocketConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: javax.websocket.server.ServerContainer not available
解决方法
修改@SpringBootTest,其中DemoApplication为启动类
@SpringBootTest(classes = DemoApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)

参考博客:https://blog.csdn.net/qq_40808344/article/details/86673721
本文详细介绍了在SpringBoot项目中整合WebSocket时遇到的测试类报错问题,错误信息显示无法创建Bean 'serverEndpointExporter',并给出了具体的解决办法。通过修改@SpringBootTest注解中的属性,确保测试环境正确加载WebSocket配置。

1107

被折叠的 条评论
为什么被折叠?



