Invocation of init method failed; nested exception is java.lang.IllegalStateException: javax.websocket.server.ServerContainer not available


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_PORT)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM