https://blog.youkuaiyun.com/chunjusu2447/article/details/100820520
在SpringBoot项目中集成了WebSocket,在进行单元测试的时候,出现了以下错误:
javax.websocket.server.ServerContainer not available
在没有进行单元测试时候,项目还可以正常启动
单元测试类的原注解是这样的:
@RunWith(SpringRunner.class)
@SpringBootTest
经查阅资料,得知SpringBootTest在启动的时候不会启动服务器,所以WebSocket自然会报错,这个时候需要添加选项webEnvironment,以便提供一个测试的web环境。如下:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
本文讲述了如何在SpringBoot项目中集成WebSocket时,遇到单元测试`javax.websocket.server.ServerContainerNotAvailable`错误,并介绍了如何通过修改`SpringBootTest`的webEnvironment属性来为测试提供适当环境,确保WebSocket功能正常工作。
4412

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



