Netty 作为 WebSocket 服务器未在 SpringBootTest 中启动
Netty as WebSocket server not starting in SpringBootTest
我正在尝试基于 Spring 和 Netty 编写一个简单的 WebSocket 服务器应用程序。
我的申请是这样的
@SpringBootApplication
public class DemoReactiveWSApp {
public static void main(String[] args) {
SpringApplication.run(DemoReactiveWSApp.class, args);
}
}
配置如下
@Configuration
public class WebSocketConfig {
@Bean
public HandlerMapping handlerMapping() {
final Map<String, WebSocketHandler> handlerMap = new HashMap<>();
// will be populated later with routes and handlers
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setUrlMap(handlerMap);
mapping.setOrder(-1);
return mapping;
}
@Bean
public RequestUpgradeStrategy requestUpgradeStrategy() {
return new ReactorNettyRequestUpgradeStrategy();
}
}
当我 运行 它时,一切都会启动,我可以尝试(暂时)建立 WS 连接。
但是,当我想在测试中启动它时
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DemoReactiveWSAppTest {
@LocalServerPort
private String port;
@Test
public void givenContext_WhenStartingApplication_ThenItLoads() throws InterruptedException {
System.out.println("Port: " + port);
}
}
服务器似乎永远无法启动。
我是不是忘记了什么?
愚蠢的我,我忘记了@RunWith(SpringRunner.class)
我正在尝试基于 Spring 和 Netty 编写一个简单的 WebSocket 服务器应用程序。
我的申请是这样的
@SpringBootApplication
public class DemoReactiveWSApp {
public static void main(String[] args) {
SpringApplication.run(DemoReactiveWSApp.class, args);
}
}
配置如下
@Configuration
public class WebSocketConfig {
@Bean
public HandlerMapping handlerMapping() {
final Map<String, WebSocketHandler> handlerMap = new HashMap<>();
// will be populated later with routes and handlers
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setUrlMap(handlerMap);
mapping.setOrder(-1);
return mapping;
}
@Bean
public RequestUpgradeStrategy requestUpgradeStrategy() {
return new ReactorNettyRequestUpgradeStrategy();
}
}
当我 运行 它时,一切都会启动,我可以尝试(暂时)建立 WS 连接。
但是,当我想在测试中启动它时
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class DemoReactiveWSAppTest {
@LocalServerPort
private String port;
@Test
public void givenContext_WhenStartingApplication_ThenItLoads() throws InterruptedException {
System.out.println("Port: " + port);
}
}
服务器似乎永远无法启动。
我是不是忘记了什么?
愚蠢的我,我忘记了@RunWith(SpringRunner.class)