由于缺少ReactiveWebServerFactory bean,无法启动ReactiveWebApplicationContext

时间:2018-05-14 12:02:07

标签: java spring-boot spring-boot-test

我有一个新的springboot应用程序,我试图开始使用。

我收到的错误是

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 /主/爪哇/ 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 /测试/ JAVA /测试/ 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();
  }
}

5 个答案:

答案 0 :(得分:9)

我假设您正在使用maven来获取依赖项。

我通过使用:

解决了这个问题
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
        <version>2.0.3.RELEASE</version>
    </dependency>

而不是:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webflux</artifactId>
        <version>5.0.7.RELEASE</version>
    </dependency>

答案 1 :(得分:6)

您的配置不足以进行反应性测试。

反应式WebTestClientReactiveWebApplicationContext在应用程序上下文中都需要反应式服务器。将注释@EnableAutoConfiguration添加到您的RootControllerTest中,让Spring为您完成。

自动配置会搜索您的类路径,并在找到反应性类和反应性上下文之后创建ReactiveWebServerFactory bean。

答案 2 :(得分:5)

可能是一个损坏的下载。尝试删除〜/ .m2 / repository。

答案 3 :(得分:2)

实际上,您只需要在webEnvironment = WebEnvironment.RANDOM_PORT批注中将webEnvironment = WebEnvironment.MOCK更改为@SpringBootTest

答案 4 :(得分:1)

对我来说,该错误是由Spring类上缺少@SpringBootAnnotation方法入口点的main()注释引起的,该注释实际上启动了Boot应用程序。使用以下命令可以解决该错误:

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
相关问题