2

WebSocketアプリケーションをテストします。 SpringBootTest @EnabledWebSocketが無視されました

テストクラス:

@RunWith(SpringRunner.class) 
@SpringBootTest(
    webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) 
@ContextConfiguration(
    classes = {WebConfig.class, WebSocketConfig.class} 
@DirtiesContext 
public class IntegrationTest { 

    @Autowired 
    public EmbeddedWebApplicationContext server; 

    @Test 
    ... 
} 

のWebConfigクラス:

@Configuration 
public class WebConfig extends WebMvcConfigurerAdapter { 

    public final WebSocketService webSocketService; 

    @Autowired 
    public WebConfig(WebSocketService webSocketService) { 
     this.webSocketService = webSocketService; 
    } 

    @Bean 
    public EmbeddedServletContainerFactory servletContainer() { 
     TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); 
     tomcat.setPort(1234); 
     tomcat.setContextPath("/test"); 

     return tomcat; 
    } 
} 

そしてWebSocketConfigクラス:私は、テストを開始すると

@Configuration 
@EnableWebSocket 
public class WebSocketConfig implements WebSocketConfigurer { 

    @Autowired 
    public WebSocketConfig() {}      

    @Override 
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {   

     webSocketHandlerRegistry.addHandler(webSocketHandler(), "ws")     
      .setAllowedOrigins("*")     
    } 

    @Bean 
    public WebSocketHandler webSocketHandler() { 
     return new WebsocketHandler(webSocketService()); 
    } 

    @Bean 
    publicWebSocketService webSocketService() { 
     return newWebSocketServiceImpl(); 
    } 
} 

は、Tomcatが起動され、待機しています指定されたポート1234にあります。しかし、私はwebsocketクライアントに接続することはできません。 WebSocketConfigが呼び出されます。しかし、私はwebsocketのマッピングが動作しないと思う。設定するものは何も忘れましたか?

@SpringBootApplicationという注釈が付けられたアプリケーションクラス(WebSocketApp.class)でテストを開始すると、websocketマッピングが正常に動作します。

@RunWith(SpringRunner.class) 
@SpringBootTest(
    webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) 
@ContextConfiguration(
    classes = {WebSocketApp.class} 
@DirtiesContext 
public class IntegrationTest { 

    @Autowired 
    public EmbeddedWebApplicationContext server; 

    @Test 
    ... 
} 

WebSocketAppも同じ設定を使用します。

@EnableWebSocketが使用されているため、2番目のアプローチが動作していると仮定します。 WebSocketApp.class(@SpringBootApplicationと注釈されています)を取らないと、@EnableWebSocketは無視されます。

誰かがテストを実行するアイデアはありますか?アノテーションなしで手動でWebソケットを有効にするにはどうすればよいですか?

EDIT:

私はTomcatEmbeddedContextは、デフォルトのサーブレットマッピングの代わりdispatcherServletマッピングが使用していることが、分かりました。このタイプのマッピングを設定することは可能ですか?

答えて

0

解決策が見つかりました。テスト設定では、Springはwebsocketコンテナとサーブレットのマッピングをブートストラップしませんでした。

@Bean 
    public ServletServerContainerFactoryBean createWebSocketContainer() { 
     ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean(); 
     container.setMaxTextMessageBufferSize(8192); 
     container.setMaxBinaryMessageBufferSize(8192); 
     return container; 
    } 

    @Bean 
    public DispatcherServlet dispatcherServlet() { 
     return new DispatcherServlet(); 
    } 

    @Bean 
    public ServletRegistrationBean dispatchServletRegistration() { 

     ServletRegistrationBean registration = new ServletRegistrationBean(
      dispatcherServlet(), "/"); 

     registration 
      .setName(DispatcherServletAutoConfiguration.DEFAULT_DISPATCHER_SERVLET_BEAN_NAME); 

     return registration; 

    } 

とBean EmbeddedServletContainerFactoryへ:

tomcat.addContextCustomizers((TomcatContextCustomizer) context -> 
      context.addServletContainerInitializer(new WsSci(), null)); 

は、私はいくつかの追加設定を追加する必要がありました
関連する問題