springboot整合websocket后运行测试类报错:javax.websocket.server.ServerContainer not available

本文介绍了如何通过在SpringBootTest注解中设置webEnvironment属性解决Spring Boot整合WebSocket时的测试类启动报错问题,详细解释了RANDOM_PORT、WEB_ENVIRONMENT_RANDOME_PORT等四个选项的作用,确保真实servlet环境和端口设置以支持WebSocket功能。
摘要由CSDN通过智能技术生成

springboot整合websocket,测试类启动报错,

解决方式:在注解中加上webEnvironment参数,此参数的含义,参看官方文档

You can use the webEnvironment attribute of @SpringBootTest to further refine how your tests will run:

MOCK — Loads a WebApplicationContext and provides a mock servlet environment. Embedded servlet containers are not started when using this annotation. If servlet APIs are not on your classpath this mode will transparently fallback to creating a regular non-web ApplicationContext. Can be used in conjunction with @AutoConfigureMockMvc for MockMvc-based testing of your application.
RANDOM_PORT — Loads an ServletWebServerApplicationContext and provides a real servlet environment. Embedded servlet containers are started and listening on a random port.
DEFINED_PORT — Loads an ServletWebServerApplicationContext and provides a real servlet environment. Embedded servlet containers are started and listening on a defined port (i.e from your application.properties or on the default port 8080).
NONE — Loads an ApplicationContext using SpringApplication but does not provide any servlet environment (mock or otherwise).
有4个value可选,根据需要选择即可,其实就是给你的测试类配置了一个真实的servlet环境,并且给这个启动的嵌入式servlet容器一个端口进行监听;

如此就可以结果websocket无法获取的问题

@SpringBootTest(classes = XXX.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_POR

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值