38

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.

Devas
  • 1,544
  • 4
  • 23
  • 28

4 Answers4

48

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

  • 20
    Also: `import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; @SpringBootTest(classes = {ServletWebServerFactoryAutoConfiguration.class, ... })` – cavpollo May 22 '18 at 02:02
  • what is WebSocketSourceIntegrationTests exactly here? – bharal Apr 09 '19 at 05:59
  • @bharal WebSocketSourceIntegrationTests is the name of the class posted in the description/question. – Andres Cespedes Morales May 17 '19 at 08:10
  • 4
    For me, this happened when I had `@Configuration` instead of `@TestConfiguration` in my test config class (listed under "classes" in the `@SpringBootTest` annotation) – Programmer Trond Dec 11 '19 at 11:11
  • 1
    Simply adding the `@TestConfiguration` annotated config class (and nothing else) solved it for me, thanks! – Blacklight Feb 10 '21 at 11:41
  • Thanks a lot! Adding `@EnableAutoConfiguration` on a test class helped me! – jvmusin Mar 19 '21 at 18:32
1

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

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 radon to radon.app fixed the issue.

Anand Rockzz
  • 6,072
  • 5
  • 64
  • 71
0

this is because spring is not able to load the properties file at runtime, i was using spring profiles 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.

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
LocoGris
  • 4,432
  • 3
  • 15
  • 30
-2

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

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

If not working check this answer: https://stackoverflow.com/a/50232382/6831257

GolamMazid Sajib
  • 8,698
  • 6
  • 21
  • 39