代码之家  ›  专栏  ›  技术社区  ›  ioreskovic

netty as websocket server未在SpringBootTest中启动

  •  0
  • ioreskovic  · 技术社区  · 6 年前

    我正在尝试基于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 回复  |  直到 6 年前
        1
  •  0
  •   ioreskovic    6 年前

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