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
マッピングが使用していることが、分かりました。このタイプのマッピングを設定することは可能ですか?