程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Spring Boot Test失败说,由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Spring Boot Test失败说,由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext?

开发过程中遇到Spring Boot Test失败说,由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext的问题如何解决?下面主要结合日常开发的经验,给出你关于Spring Boot Test失败说,由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext的解决方法建议,希望对你解决Spring Boot Test失败说,由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext有所启发或帮助;

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

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

解决方案

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

WebsocketsourceConfiguration {
  @Bean 
  ServletWebServerFactory servletWebServerFactory(){
  return new tomcatServletWebServerFactory();
  }
}

或还使自动配置能够对这些bean进行类路径扫描和自动配置。

@EnableautoConfiguration
WebsocketsourceConfiguration

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

@EnableautoConfiguration
WebSocketsourceIntegrationTests

解决方法

测试类别:

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

}

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

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

大佬总结

以上是大佬教程为你收集整理的Spring Boot Test失败说,由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext全部内容,希望文章能够帮你解决Spring Boot Test失败说,由于缺少ServletWebServerFactory bean而无法启动ServletWebServerApplicationContext所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。