unit-testing - 如何在测试时使 Spring Boot webflux 在默认 Netty 而不是 Jetty 上启动

标签 unit-testing spring-boot amazon-dynamodb spring-webflux

我有一个有点类似的 gradle 和 spring webflux 配置,如 Why spring webflux is choosing jetty by default and then failing? 中所述。

但是我们的目标是在内存中使用 Amazon dynamo db + spring webflux on netty(而不是 jetty)来进行单元测试(我们已经在 netty 上拥有带有 spring webfux 的生产 dynamo db)。我能够使用内存中的 dynamo db 运行单元测试,但是一旦我启用 springboot webflux,它就会开始提示:

   Caused by: java.lang.NoClassDefFoundError: org/eclipse/jetty/servlet/ServletHolder
    at org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory.createJettyServer(JettyReactiveWebServerFactory.java:176) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory.getWebServer(JettyReactiveWebServerFactory.java:106) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext$ServerManager.<init>(ReactiveWebServerApplicationContext.java:202) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext$ServerManager.get(ReactiveWebServerApplicationContext.java:221) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.createWebServer(ReactiveWebServerApplicationContext.java:90) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.onRefresh(ReactiveWebServerApplicationContext.java:79) ~[spring-boot-2.1.5.RELEASE.jar:2.1.5.RELEASE]
    ... 62 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.servlet.ServletHolder

我在 build.gradle 中尝试过以下操作:
 configurations {
    // exclude Reactor Jetty /Tomcat 
    compile.exclude  group: 'org.springframework.boot',module: 'spring-boot-starter-jetty'
    testcompile.exclude  group: 'org.springframework.boot',module: 'spring-boot-starter-jetty'
    //compile.exclude group: 'javax.servlet' , module: 'servlet-api' //<--tried servlet jar exclusion also
}

.....
testCompile ('com.amazonaws:DynamoDBLocal:1.11.477')
....//event tried this ( and is probably wrong)
testCompile("org.springframework.boot:spring-boot-starter-test:$springBootVersion") {
        exclude  group: 'org.springframework.boot', module: 'spring-boot-starter-jetty' //by both name and group
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'

    }

我检查了依赖关系图:
\--- com.amazonaws:DynamoDBLocal:1.11.477
    +--- org.antlr:antlr4-runtime:4.7.2
    +--- commons-cli:commons-cli:1.2
    +--- org.apache.commons:commons-lang3:3.8.1
    +--- com.almworks.sqlite4java:libsqlite4java-linux-i386:1.0.392
    |    \--- com.almworks.sqlite4java:sqlite4java:1.0.392
    +--- com.almworks.sqlite4java:libsqlite4java-linux-amd64:1.0.392
    |    \--- com.almworks.sqlite4java:sqlite4java:1.0.392
    +--- com.almworks.sqlite4java:sqlite4java-win32-x64:1.0.392
    |    \--- com.almworks.sqlite4java:sqlite4java:1.0.392
    +--- com.almworks.sqlite4java:sqlite4java-win32-x86:1.0.392
    |    \--- com.almworks.sqlite4java:sqlite4java:1.0.392
    +--- com.almworks.sqlite4java:libsqlite4java-osx:1.0.392
    |    \--- com.almworks.sqlite4java:sqlite4java:1.0.392
    +--- com.amazonaws:aws-java-sdk-core:1.11.477 (*)
    +--- com.amazonaws:aws-java-sdk-dynamodb:1.11.477 (*)
    +--- org.apache.logging.log4j:log4j-api:2.6.2 -> 2.11.2
    +--- org.apache.logging.log4j:log4j-core:2.6.2 -> 2.11.2
    |    \--- org.apache.logging.log4j:log4j-api:2.11.2
      +--- org.eclipse.jetty:jetty-client:8.1.12.v20130726 -> 9.4.18.v20190429
    |    +--- org.eclipse.jetty:jetty-http:9.4.18.v20190429
    |    |    +--- org.eclipse.jetty:jetty-util:9.4.18.v20190429
    |    |    \--- org.eclipse.jetty:jetty-io:9.4.18.v20190429
    |    |         \--- org.eclipse.jetty:jetty-util:9.4.18.v20190429
    |    \--- org.eclipse.jetty:jetty-io:9.4.18.v20190429 (*)
    +--- org.eclipse.jetty:jetty-server:8.1.12.v20130726 -> 9.4.18.v20190429
    |    +--- javax.servlet:javax.servlet-api:3.1.0 -> 4.0.1
    |    +--- org.eclipse.jetty:jetty-http:9.4.18.v20190429 (*)
    |    \--- org.eclipse.jetty:jetty-io:9.4.18.v20190429 (*)
    +--- org.mockito:mockito-core:1.10.19 -> 2.23.4
    |    +--- net.bytebuddy:byte-buddy:1.9.3 -> 1.9.12


所以我需要的是强制 webflux 将 Netty 作为服务器。我认为它正在被 dynamodb 依赖或一些 jar 传递性覆盖。

最佳答案

我遇到了这个完全相同的问题。问题在于DynamoDBLocal库可传递地引用 Jetty。由于 Jetty 和 Netty 都在类路径上,Webflux 认为它应该使用 Jetty 而不是 Netty 来启动。我能够按照此处 Spring Boot 文档中的详细说明解决此问题:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-embedded-web-servers.html#howto-configure-webserver

具体这部分:

As a last resort, you can also declare your own WebServerFactory component, which will override the one provided by Spring Boot.



更具体地说,您可以将其添加到您的测试配置(或您的整个应用程序配置,如果您想明确告诉所有内容始终使用 Netty):
@Bean
public ReactiveWebServerFactory reactiveWebServerFactory() {
    return new NettyReactiveWebServerFactory();
}

关于unit-testing - 如何在测试时使 Spring Boot webflux 在默认 Netty 而不是 Jetty 上启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56783116/

相关文章:

java - DynamoDB映射器仅保存项目Java中的一个属性

python-2.7 - aws DynamoDB boto3 查询 GROUP BY 和 ORDER BY

amazon-web-services - 在不同的 AWS 账户中查询 Dynamodb - Serverless Framework

c# - 如何使用 Moq 模拟数据库方法调用?

unit-testing - 使用 JavaFx 和 TestFx 进行 headless 测试

c# - 单元测试服务层和 Entity Framework 6

java - Zipkin客户端无法连接到Zipkin服务器

java - Spring Boot Actuator不读取git.properties

java - Spring中如何模拟私有(private)方法中调用的公共(public)方法的返回

java - 如何使用Spring Boot并行执行SQL查询?