【问题标题】:SpringBootTest @EnabledWebSocket ignoredSpringBootTest @EnabledWebSocket 被忽略
【发布时间】:2017-04-20 15:32:04
【问题描述】:

我想测试我的 WebSocket 应用程序。

测试类:

@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。而当我不使用 WebSocketApp.class(用 @SpringBootApplication 注释)时,@EnableWebSocket 将被忽略。

有人有想法让测试运行吗?如何在没有注释的情况下手动启用 websocket?

编辑:

我发现,TomcatEmbeddedContext 使用的是默认 servlet 映射,而不是 dispatcherServlet 映射。是否可以设置这种类型的映射?

【问题讨论】:

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


    【解决方案1】:

    我找到了解决方案。在测试配置中,Spring 没有引导 websocket 容器和 servlet 映射。

    我不得不添加一些额外的配置:

    @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;
    
        }
    

    还有EmbeddedServletContainerFactory豆:

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

    【讨论】:

      【解决方案2】:

      在我的情况下,在我的 Spring 引导测试类中添加 @EnableAutoConfiguration 可以解决此问题。 Spring 初始化 DispatcherServlet 并正确配置它

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-26
        • 2016-06-05
        • 2014-02-25
        • 2011-05-09
        • 2015-02-08
        • 2011-03-27
        相关资源
        最近更新 更多