Netty作为WebSocket服务器无法在SpringBootTest中启动

时间:2018-06-19 07:58:22

标签: java spring-boot netty spring-test spring-websocket

我正在尝试编写一个基于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);
    }
}

服务器似乎永远不会启动。

我忘记了什么吗?

1 个答案:

答案 0 :(得分:0)

愚蠢的我,我忘了@RunWith(SpringRunner.class)