【发布时间】:2016-10-20 08:04:35
【问题描述】:
我不明白如何正确地在 springboot 应用程序中为 websocket 编写测试用例。我有一个实现WebSocketHandler 的类,我在WebSocketConfigurer 中添加了这个处理程序:
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(logWebSocketHandler() , "/test")
.addInterceptors(new HttpSessionHandshakeInterceptor())
.setAllowedOrigins("*");
}
@Bean
public LogWebSocketHandler logWebSocketHandler(){
return new LogWebSocketHandler();
}
}
但是当我在下面的测试用例中写这个时,我得到了一个异常:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration({App.class})
@WebAppConfiguration
public class LogsControllerTest {
private WebSocketContainer container;
@Before
public void setup() {
container = ContainerProvider.getWebSocketContainer();
}
@Test
public void testGetLog() throws Exception {
Session session = container.connectToServer(new TestEndpoint(),
URI.create("ws://127.0.0.1:8080/test"));
}
}
例外:
javax.websocket.DeploymentException: 发起WebSocket连接的HTTP请求失败
我读到如果我设置ws://127.0.0.1:8080/test/(斜线结尾)它会起作用,但它不起作用。
我做错了什么?
【问题讨论】:
标签: java spring spring-boot spring-websocket java-websocket