Spring 启动测试失败说,由于缺少 ServletWebServerFactory bean,无法启动 ServletWebServerApplicationContext

Spring boot Test fails saying, Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean

测试Class:-

@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****************************************************");
    }

}

我遇到了同样的问题 here 但没有任何帮助。我可以知道我错过了什么吗?

我在依赖层次结构中有 spring-boot-starter-tomcat 作为编译时依赖。

这条消息说: 您需要在 ApplicationContext 中配置至少 1 个 ServletWebServerFactory bean,所以如果您已经有 spring-boot-starter-tomcat you需要自动配置该 bean 或手动配置.

所以,在测试中只有2个配置classes来加载applicationContext,这些是= { WebsocketSourceConfiguration.class, WebSocketSourceIntegrationTests.class },那么至少在其中一个class是的,应该有一个返回所需 ServletWebServerFactory 实例的 @Bean 方法。

* 解决方案 *

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

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

或者还启用自动配置以执行 class路径扫描和这些 bean 的自动配置。

@EnableAutoConfiguration
WebsocketSourceConfiguration

也可以在集成测试中完成 class。

@EnableAutoConfiguration
WebSocketSourceIntegrationTests

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

2.0.5.RELEASE 中,我在遇到以下问题时遇到了类似的问题。

package radon;
..
@SpringBootApplication
public class Initializer {
    public static void main(String[] args) {
        SpringApplication.run(Config.class, args);
    }
}

package radon.app.config;
@Configuration
@ComponentScan({ "radon.app" })
public class Config {
    ..
}

将 Initializer 包从 radon 更改为 radon.app 解决了这个问题。

这是因为 spring 无法在 runtime 加载属性文件,我正在使用 spring 配置文件并且没有在 spring 提供(程序或虚拟机)参数runtime( java -jar application.jar) ,添加配置文件的 vm 参数解决了我的问题。

java -jar -Dspring.profiles.active=dev application.jar

或使用程序参数

java -jar application.jar --spring.profiles.active=prod --spring.config.location=c:\config

对于 Web 应用程序,在主 class 中扩展 *SpringBootServletInitializer*

@SpringBootApplication
public class YourAppliationName extends SpringBootServletInitializer{
    public static void main(String[] args) {
        SpringApplication.run(YourAppliationName.class, args);
    }
}