Springboot运行测试类对新模块进行测试的时候报错:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'serverEndpointExporter' defined in class path resource [**/WebSocketConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: javax.websocket.server.ServerContainer not available
最近项目新加入的模块就是websocket,再之后就是今天进行测试的代码。根据报错位置跳转的到地方确定应该是websocket的问题。仔细查看报错信息发现是因为服务器容器没有找到,然后去找答案,原来websocket想要启动必须要一个真的服务器容器或者说是tomcat等容器,而测试类显然没有这一块。 所以在测试过程中我们要真正的启动一个tomcat作为容器。
即在SpringBootTest后加上
(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 即可