jetty - 使用 gradle 进行集成测试

标签 jetty gradle integration-testing

我是 gradle 工具的新手,我正在尝试使用 gradle 中的 jetty 插件添加对运行集成测试的支持。
我遇到了两个帖子,onetwo ,但我的方法的不同之处在于,我没有使用单独的源集进行集成测试。我的集成测试按照模式命名,

" **/ITCase*.*".



我的项目有 4 个子模块,我正在为服务模块设置集成测试。使用示例 here ,我设置了 jetty 配置:
[jettyRun, jettyStop]*.stopPort = 8006
[jettyRun,jettyStop]*.stopKey = 'STOP'
jettyRun {
   classpath=configurations.compile
}

task integrationTest(type: Test, dependsOn: "test" ) {
include '**/*ITCase*.*'
println 'Starting the embedded jetty'
doFirst {
    jettyRun.daemon = true
    jettyRun.scanIntervalSeconds = 0
    jettyRun.execute()
}
doLast {
    jettyStop.execute()
    println 'Stopping the embedded jetty'
}

}

“test”是在父项目的 build.gradle 中定义的任务。

当我运行命令时, gradle war integrationTest ,集成测试失败,因为它在服务项目中找不到上下文监听器。
错误消息的片段如下:
:services:integrationTest (Thread[Daemon Thread 14,5,main]) started.
:services:integrationTest
Executing task ':services:integrationTest' (up-to-date check took 0.026 secs) due to:
  No history is available.
Starting the embedded jetty
Executing task ':services:jettyRun' (up-to-date check took 0.0 secs) due to:
  Task has not declared any outputs.
Configuring Jetty for project ':services'
Webapp source directory = E:\Workspace\project\services\src\main\webapp
Reload Mechanic: automatic
web.xml file = E:\Workspace\project\services\src\main\webapp\WEB-INF\web.xml
Context path = /services
Tmp directory =  determined at runtime
Web defaults = org/mortbay/jetty/webapp/webdefault.xml
Web overrides =  none
Webapp directory = E:\Workspace\project\services\src\main\webapp
Starting jetty 6.1.25 ...
jetty-6.1.25
Could not instantiate listener com.db.project.services.ProjectServletContextListener
java.lang.ClassNotFoundException: com.db.project.services.ProjectServletContextListener
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at org.mortbay.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:401)
    at org.mortbay.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:363)
    at org.mortbay.jetty.handler.ContextHandler.loadClass(ContextHandler.java:1101)
    at org.mortbay.jetty.webapp.WebXmlConfiguration.initListener(WebXmlConfiguration.java:630)
    at org.mortbay.jetty.webapp.WebXmlConfiguration.initWebXmlElement(WebXmlConfiguration.java:368)
    at org.mortbay.jetty.plus.webapp.AbstractConfiguration.initWebXmlElement(AbstractConfiguration.java:190)
    at org.mortbay.jetty.webapp.WebXmlConfiguration.initialize(WebXmlConfiguration.java:289)

对此的任何帮助表示赞赏。
提前致谢

最佳答案

我认为您的错误消息的根本原因是,您的 jetty 任务的类路径不包含项目生产代码,而只是它的编译类路径。为了让它工作,从片段中删除类路径配置应该可以解决问题。 jetty 插件已经为您做到了。

另一个注意事项:通常,通过运行“task.execute”来执行任务被认为是不好的做法。相反,您应该使用 Task#dependsOn 和 Task#finalizedBy。想象一下,您的一项测试失败了。那么 doLast block 永远不会执行,您的 jetty 也永远不会停止。

干杯,
勒内

关于jetty - 使用 gradle 进行集成测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20926251/

相关文章:

java - 在集成测试中重写@Value

java - 使用 DropWizard 处理长请求

java - 内存不足错误: Direct buffer memory in jetty websocket with 32 Bit

java - 如何在其他模块的源集中添加 proto 生成的文件?

java - 如何使用文件位置枚举多模块 Gradle 项目中的所有模块?

java - Cucumber-junit-platform-engine 中的特征文件发现

tomcat - google-oauth-client-jetty 无法在 Tomcat 7 上运行

java - Apache HttpComponents 的替代品?

flutter - 使用 Dart::ffi 在带有 native 函数的 flutter 包中测试库函数

ruby-on-rails - 带有子域的 capybara - default_host