Spring Boot Test失败说,由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext [英] Spring boot Test fails saying, Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

查看:768
本文介绍了Spring Boot Test失败说,由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

测试类:-

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { WebsocketSourceConfiguration.class,
        WebSocketSourceIntegrationTests.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {
                "websocket.path=/some_websocket_path", "websocket.allowedOrigins=*",
                "spring.cloud.stream.default-binder=kafka" })
public class WebSocketSourceIntegrationTests {

    private String port = "8080";

    @Test
    public void testWebSocketStreamSource() throws IOException, InterruptedException {
        StandardWebSocketClient webSocketClient = new StandardWebSocketClient();
        ClientWebSocketContainer clientWebSocketContainer = new ClientWebSocketContainer(webSocketClient,
                "ws://localhost:" + port + "/some_websocket_path");
        clientWebSocketContainer.start();
        WebSocketSession session = clientWebSocketContainer.getSession(null);
        session.sendMessage(new TextMessage("foo"));
        System.out.println("Done****************************************************");
    }

}

我在此处遇到了同样的问题,但没有任何帮助我.我可以知道我在想什么吗?

I have seen same issue here but nothing helped me. May I know what I'm missing ?

在依赖项层次结构中,我将spring-boot-starter-tomcat作为编译时间依赖项.

I have spring-boot-starter-tomcat as compile time dependency in the dependency Hierarchy.

推荐答案

此消息说: 您需要在ApplicationContext中配置至少1个ServletWebServerFactory bean ,因此,如果您已经拥有spring-boot-starter-tomcat,则您需要自动配置该bean或手动进行.

This message says: You need to configure at least 1 ServletWebServerFactory bean in the ApplicationContext, so if you already have spring-boot-starter-tomcat you need to either autoconfigure that bean or to do it manually.

因此,在测试中,只有2个配置类可加载applicationContext,它们是= {WebsocketSourceConfiguration.class,WebSocketSourceIntegrationTests.class},然后至少在这些类之一中,应该有一个@Bean方法返回实例所需的ServletWebServerFactory.

So, in the test there are only 2 configuration classes to load the applicationContext, these are = { WebsocketSourceConfiguration.class, WebSocketSourceIntegrationTests.class }, then at least in one of these classes there should be a @Bean method returning an instance of the desired ServletWebServerFactory.

*解决方案*

确保加载配置类中的所有bean

Make sure to load all the beans within your configuration class

WebsocketSourceConfiguration {
  @Bean 
  ServletWebServerFactory servletWebServerFactory(){
  return new TomcatServletWebServerFactory();
  }
}

OR还使AutoConfiguration能够对那些bean进行类路径扫描和自动配置.

OR also enable the AutoConfiguration to do a classpath scanning and auto-configuration of those beans.

@EnableAutoConfiguration
WebsocketSourceConfiguration

也可以在集成测试课程中完成.

Can be done also at the Integration Test class.

@EnableAutoConfiguration
WebSocketSourceIntegrationTests

有关更多信息,请参见 SpringBootTest 批注文档 https://docs .spring.io/spring-boot/docs/current/api/org/springframework/boot/test/context/SpringBootTest.html

For more information check the SpringBootTest annotation documentation https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/context/SpringBootTest.html

这篇关于Spring Boot Test失败说,由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆