Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 here but nothing helped me. May I know what I'm missing ?

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

like image 528
Krishas Avatar asked Jan 15 '18 13:01

Krishas


People also ask

What is ServletWebServerFactory?

Interface ServletWebServerFactory This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference. @FunctionalInterface public interface ServletWebServerFactory extends WebServerFactory. Factory interface that can be used to create a WebServer .

What is ServletWebServerApplicationContext?

public class ServletWebServerApplicationContext extends GenericWebApplicationContext implements ConfigurableWebServerApplicationContext. A WebApplicationContext that can be used to bootstrap itself from a contained ServletWebServerFactory bean.

How do I test a bean spring?

To test whether Spring MVC controllers are working as expected, use the @WebMvcTest annotation. To test that Spring WebFlux controllers are working as expected, you can use the @WebFluxTest annotation. You can use the @DataJpaTest annotation to test JPA applications.


1 Answers

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.

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.

* SOLUTION *

Make sure to load all the beans within your configuration class

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

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 

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

like image 116
Andres Cespedes Morales Avatar answered Sep 19 '22 15:09

Andres Cespedes Morales