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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48264118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:48:33  来源:igfitidea点击:

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

javaspringspring-bootspring-cloud-stream

提问by Krishas

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

}

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

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

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

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

采纳答案by Andres Cespedes Morales

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.

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

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.

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

* SOLUTION *

* 解决方案 *

Make sure to load all the beans within your configuration class

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

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

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

或者还启用 AutoConfiguration 对这些 bean 进行类路径扫描和自动配置。

@EnableAutoConfiguration
WebsocketSourceConfiguration

Can be done also at the Integration Test class.

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

@EnableAutoConfiguration
WebSocketSourceIntegrationTests

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

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

回答by Anand Rockzz

in 2.0.5.RELEASEi faced a similar issue when I had the following.

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 {
    ..
}

Changing the package of Initializer from radonto radon.appfixed the issue.

将 Initializer 包从 更改radonradon.app修复了该问题。

回答by Sanjay Nayak

this is because springis not able to load the properties file at runtime, i was using springprofiles and wasn't providing the (program or vm) argument at runtime( java -jar application.jar), adding vm argument of profile resolved the issue for me.

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

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

or using program argument

或使用程序参数

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

回答by GolamMazid Sajib

For web applications, extends *SpringBootServletInitializer*in main class.

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

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