4

我有一个通过 Spring boot 2.0.0.M3 使用 Spring Webflux 的应用程序。

应用程序在运行时设置为REACTIVE类型。

public static void main(String[] args) {
    SpringApplication application = new SpringApplication(AgentApplication.class);
    application.setWebApplicationType(WebApplicationType.REACTIVE);
    application.run(args);
}

如果运行主应用程序,反应式应用程序可以正常工作。但我未能在我的 Spring Boot 集成测试中启动这个应用程序。

我宣布测试如下,

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
    properties = {"spring.main.webApplicationType=reactive"})
@ActiveProfiles("integTest")
public class NoteHandlerTest {

@Autowired
private WebTestClient webClient;

@Test
public void testGetNoteNotFound() throws Exception {
    this.webClient.get().uri("/note/request/{id}", "nosuchid").accept(MediaType.APPLICATION_JSON_UTF8)
            .exchange().expectStatus().isOk();
}

}

运行测试用例时出现以下错误,

java.lang.IllegalStateException:无法加载 ApplicationContext

intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51) 在 com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) 在 com.intellij.rt.execution。 junit.JUnitStarter.main(JUnitStarter.java:70) 原因:org.springframework.context.ApplicationContextException:无法启动反应式网络服务器;嵌套异常是 org.springframework.context.ApplicationContextException:由于缺少 ReactiveWebServerFactory bean,无法启动 ReactiveWebApplicationContext。在 org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.onRefresh(ReactiveWebServerApplicationContext.java:64) 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) 在 org.springframework.boot。由于缺少 ReactiveWebServerFactory bean,无法启动 ReactiveWebApplicationContext。 在 org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.getWebServerFactory(ReactiveWebServerApplicationContext.java:103) 在 org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.createWebServer(ReactiveWebServerApplicationContext.java:87) 在 org.springframework .boot.web.reactive.context.ReactiveWebServerApplicationContext.onRefresh(ReactiveWebServerApplicationContext.java:61) ... 44 更多

使用 webflux 运行 spring boot 集成测试是否有任何配置问题?

您可以从这里访问完整的演示项目。

4

2 回答 2

3

这不只是您的配置属性中的错字吗?

"spring.main.web-application-type=reactive"

于 2017-11-16T09:38:11.827 回答
1

使用时我也有这个异常

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

代替

@SpringBootTest()
于 2017-12-01T12:21:23.353 回答