Java Spring启动测试失败,原因是缺少ServletWebServerFactory bean,无法启动ServletWebServerApplicationContext
测试等级:-Java Spring启动测试失败,原因是缺少ServletWebServerFactory bean,无法启动ServletWebServerApplicationContext,java,spring,spring-boot,spring-cloud-stream,Java,Spring,Spring Boot,Spring Cloud Stream,测试等级:- @RunWith(SpringRunner.class) @SpringBootTest(classes = { WebsocketSourceConfiguration.class, WebSocketSourceIntegrationTests.class }, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { "websocket
@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
作为编译时依赖关系。此消息显示:
您需要在ApplicationContext中至少配置1个ServletWebServerFactory bean,因此,如果您已经有spring boot starter tomcat y,则需要自动配置该bean或手动配置该bean
因此,在测试中只有两个配置类来加载applicationContext,它们是={WebsocketSourceConfiguration.class,WebSocketSourceIntegrationTests.class},那么至少在其中一个类中应该有一个@Bean方法返回所需ServletWebServerFactory的实例
*解决方案*
确保加载配置类中的所有bean
WebsocketSourceConfiguration {
@Bean
ServletWebServerFactory servletWebServerFactory(){
return new TomcatServletWebServerFactory();
}
}
或者也可以启用自动配置来对这些bean进行类路径扫描和自动配置
@EnableAutoConfiguration
WebsocketSourceConfiguration
也可以在集成测试类中完成
@EnableAutoConfiguration
WebSocketSourceIntegrationTests
有关更多信息,请查看SpringBootTest注释文档
在2.0.5.版本中我遇到了一个类似的问题,当时我遇到了以下问题
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 {
..
}
将初始值设定项包从
radon
更改为radon.app
解决了这个问题。这是因为spring
无法在运行时加载属性文件,我使用的是spring
配置文件,在运行时(java-jar-application.jar)没有提供(program或vm)参数
,添加profile的vm参数为我解决了这个问题
java -jar -Dspring.profiles.active=dev application.jar
或者使用程序参数
java -jar application.jar --spring.profiles.active=prod --spring.config.location=c:\config
对于web应用程序,在主类中扩展*SpringBootServletInitializer*
。
另外:import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration@SpringBootTest(classes={ServletWebServerFactoryAutoConfiguration.class,…})
这里到底是什么WebSocketSourceIntegrationTests?@bharal WebSocketSourceIntegrationTests是描述/问题中发布的类的名称。对我来说,这是在我的测试配置类中使用@Configuration
而不是@TestConfiguration
时发生的(在@springbootest
注释的“classes”下列出),只需添加@TestConfiguration
注释的配置类(没有其他内容)就可以解决问题,谢谢!尽可能使用正确的拼写、标点符号和语法。即使尝试了这个方法,也会遇到同样的问题
@SpringBootApplication
public class YourAppliationName extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(YourAppliationName.class, args);
}
}