【问题标题】:Running tests from JAR dependency从 JAR 依赖项运行测试
【发布时间】:2017-12-13 10:49:01
【问题描述】:

我正在使用我的测试代码创建一个 JAR,并计划从不同的项目运行测试。

以下是我正在尝试实施的方法。

项目 B:

configurations { testApi }
task testJar ....
artifacts { testApi testJar }

项目A:

dependencies {
    testRuntime "xx.xxx.xxx.projectName", configuration: "integtest";
}

测试似乎没有使用这种方法运行。关于可能是什么问题的任何想法?有更好的方法吗?

【问题讨论】:

  • 请详细说明,例如。 G。什么是“未运行”你得到什么错误,等等。
  • 当我运行 gradle test 时,测试类没有从 jar 中得到识别。但构建成功完成

标签: java gradle junit


【解决方案1】:

Gradle 仅在通过 testClassesDirs 属性配置的目录中查找测试类,该属性通过相应的 Test-type 任务配置,java 插件对于 test 任务是 project.sourceSets.test.output.classesDirs。不支持从依赖项中的 JAR 文件运行测试。

您需要提取 JAR 文件并将提取的目录添加到此属性。我认为类似以下构造的东西应该可以工作,但我没有对其进行测试:

configurations {
    externalTests
}
dependencies {
    externalTests "xx.xxx.xxx.projectName", configuration: "integtest"
    testRuntime "xx.xxx.xxx.projectName", configuration: "integtest"
}
test {
    // if only one dependency in externalTests you can use the simpler, it will fail if there are multiple dependencies
    testClassesDirs += zipTree(configurations.externalTests.singleFile)
    // if multiple dependencies in externalTests you need to use
    testClassesDirs += configurations.externalTests.files.collect { zipTree it }.sum()
}

【讨论】:

  • 谢谢@Vampire,我在尝试上述方法时遇到initializationError java.lang.ClassNotFoundException,我猜它无法识别类。有任何想法吗?我尝试解压 JAR 并设置 testClassesDir = files("${buildDir}/integTest/classes/java")。这也导致了同样的错误。
  • 然后像你原来一样把它放到类路径中,我相应地更新了我的答案。
  • 似乎这也行不通。任何关于使用从 JAR 中提取的文件的建议,我都在尝试使用 testClassesDir = files("${buildDir}/integTest/classes/java"),这也会导致同样的错误。
  • 除了不推荐使用testClsssesDir(没有尾随s)之外,请确保您的testJar 任务表现良好。该文件的结构是什么?包含类名的确切错误消息是什么?
猜你喜欢
  • 2014-02-21
  • 2012-08-23
  • 1970-01-01
  • 2013-07-17
  • 1970-01-01
  • 1970-01-01
  • 2017-06-04
  • 1970-01-01
  • 2012-05-16
相关资源
最近更新 更多