Unable to start ReactiveWebApplicationContext due to missing ReactiveWebServerFactory bean
Asked Answered
G

10

30

I have a new springboot application I am attempting to get started.

The error I receive is

org.springframework.context.ApplicationContextException: Unable to start reactive web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ReactiveWebApplicationContext due to missing ReactiveWebServerFactory bean.
    at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.onRefresh(ReactiveWebServerApplicationContext.java:76) ~[spring-boot-2.0.1.RELEASE.jar:2.0.1.RELEASE]

src/main/java/bubbleshadow/RootController.java

package bubbleshadow;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@RestController
public class RootController {
  public RootController() {

  }

  @GetMapping("/")
  public Mono<HttpStatus> returnOk() {
    return Mono.just(HttpStatus.OK);
  }
}

src/test/java/test/bubbleshadow/RootControllerTest.java

package test.bubbleshadow;
import bubbleshadow.RootController;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
// import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes=RootController.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class RootControllerTest {
  @Autowired
  WebTestClient webTestClient;

  @Test
  public void baseRouteShouldReturnStatusOK() {
    webTestClient.head().uri("/").exchange().expectStatus().isOk();
  }
}
Glandular answered 14/5, 2018 at 12:2 Comment(0)
C
19

Your configuration is not sufficient for reactive tests.

The reactive WebTestClient as well as ReactiveWebApplicationContext need reactive server in the application context. Add annotation @EnableAutoConfiguration to your RootControllerTest and let Spring's do it for you.

The autoconfiguration searches your class path and after find reactive classes and reactive context then create ReactiveWebServerFactory bean.

Claque answered 23/9, 2018 at 21:16 Comment(2)
I'm using Spring Boot version 2.2.5.RELEASE, and I also had to add the classes explicitly to the @SpringBootTest annotation (even though they were annotated with @Configuration and @Component respectively): @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {FooConfig.class, FooHandler.class})Stodge
I guess maybe a @ComponentScan annotation with the appropriate packages might do the trick as well.Stodge
T
15

I assume you are using maven to get your dependencies.

I solved the problem by using:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
        <version>2.0.3.RELEASE</version>
    </dependency>

Instead of:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webflux</artifactId>
        <version>5.0.7.RELEASE</version>
    </dependency>
Tmesis answered 17/6, 2018 at 16:43 Comment(2)
Did you try Aniket solution ? Removing your ~/.m2/repository ?Vicennial
I was able to solve that by adding a @ComponentScan to the Application or ConfigurationCarducci
C
11

For me, the error was being caused by a missing @SpringBootApplication annotation on the Spring class containing the main() method entry point which actually starts the Boot application. Using the following resolved the error:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Conchoidal answered 4/3, 2019 at 3:51 Comment(0)
A
5

You actually just need to change webEnvironment = WebEnvironment.RANDOM_PORT to webEnvironment = WebEnvironment.MOCK in your @SpringBootTest annotation.

Abadan answered 28/5, 2020 at 14:0 Comment(0)
S
4

Likely a corrupt download. Try removing ~/.m2/repository.

Skvorak answered 17/6, 2018 at 17:10 Comment(0)
D
1

@vdou's answer helped me to resolve my issue.

In addition to adding @EnableAutoConfiguration, I also had to manually add the spring application type:

spring:
  main:
    web-application-type: reactive

There is obviously something in my dependencies that is causing Spring not to be able to discover the type.

I hope this helps somebody...

Dumah answered 31/5, 2022 at 7:36 Comment(0)
B
0

If you are using Kotlin, check if in your Application class that contains the main method, doesnt have this:

runApplication<Application>{
    webApplicationType = WebApplicationType.REACTIVE
}

Then change the "REACTIVE" to "SERVELET", will work like a charm.

Baron answered 22/6, 2022 at 20:30 Comment(0)
I
0

If None of the above solutions work, try adding

@ContextConfiguration(loader = AnnotationConfigContextLoader.class)

It may help you

import org.springframework.test.context.support.AnnotationConfigContextLoader;
Indigent answered 16/11, 2022 at 9:56 Comment(0)
R
0

Yet another reason this can occur is if you're importing in a configuration class for your test that is not marked with @TestConfiguration annotation

Riparian answered 30/11, 2022 at 14:28 Comment(0)
W
0

I was getting the same problem and it was fixed by doing -

Added in build.gradle -

implementation  "org.springframework.boot:spring-boot-starter-webflux"

Added in application.properties -

spring.main.web-application-type = REACTIVE

And compatibility could be the problem as well so using spring boot version 3.2.0 and springCloudVersion version as "2023.0.0" Is still unable to fix the issue with above configurations, try to install Intellij Idea and it would work fine. Use Java 17 for the same. If it fixes your problem hit up button.

Whiting answered 17/3 at 4:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.