【问题标题】:WebTestClient does not work when using spring-webflux使用 spring-webflux 时 WebTestClient 不起作用
【发布时间】:2018-01-08 00:54:31
【问题描述】:

我通过 Spring boot 2.0.0.M3 使用 Spring webflux。下面是我项目的依赖,

dependencies {
compile 'org.springframework.boot:spring-boot-starter-actuator',
        'org.springframework.cloud:spring-cloud-starter-config',
        'org.springframework.cloud:spring-cloud-sleuth-stream',
        'org.springframework.cloud:spring-cloud-starter-sleuth',
        'org.springframework.cloud:spring-cloud-starter-stream-rabbit',
        'org.springframework.boot:spring-boot-starter-data-mongodb-reactive',
        'org.springframework.boot:spring-boot-starter-data-redis-reactive',
        'org.springframework.boot:spring-boot-starter-integration',
        "org.springframework.integration:spring-integration-amqp",
        "org.springframework.integration:spring-integration-mongodb",
        'org.springframework.retry:spring-retry',
        'org.springframework.boot:spring-boot-starter-webflux',
        'org.springframework.boot:spring-boot-starter-reactor-netty',
        'com.fasterxml.jackson.datatype:jackson-datatype-joda',
        'joda-time:joda-time:2.9.9',
        'org.javamoney:moneta:1.0',
        'com.squareup.okhttp3:okhttp:3.8.1',
        'org.apache.commons:commons-lang3:3.5'
compileOnly 'org.projectlombok:lombok:1.16.18'
testCompile 'org.springframework.boot:spring-boot-starter-test',
        'io.projectreactor:reactor-test',
        'org.apache.qpid:qpid-broker:6.1.2',
        'de.flapdoodle.embed:de.flapdoodle.embed.mongo'
}

通过./gradlew bootRun或直接运行主应用程序运行良好。

但是由于以下错误,我未能开始集成测试。

原因:org.springframework.boot.web.server.WebServerException:无法启动嵌入式Tomcat

我想知道为什么 WebTestClient 仍然尝试使用嵌入式 tomcat,即使我们使用的是默认使用响应式网络的 webflux。

是spring boot test的配置错误还是bug?

下面是我的测试用例的代码sn-p,

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class NoteHandlerTest {

@Autowired
private WebTestClient webClient;

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

【问题讨论】:

    标签: spring spring-boot spring-test spring-boot-test spring-webflux


    【解决方案1】:

    启动tomcat的错误是由测试依赖org.apache.qpid:qpid-broker:6.1.2引起的,依赖javax.serlvet-api:3.1破坏tomcat的启动。

    排除下面无用的模块,让tomcat重新启动,

    configurations {
        all*.exclude module: 'qpid-broker-plugins-management-http'
        all*.exclude module: 'qpid-broker-plugins-websocket'
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-24
      • 2020-10-24
      • 1970-01-01
      • 1970-01-01
      • 2020-04-23
      • 2017-08-02
      • 2018-09-06
      • 2022-12-15
      相关资源
      最近更新 更多