Java 从JAR依赖项运行测试

Java 从JAR依赖项运行测试,java,gradle,junit,Java,Gradle,Junit,我正在用我的测试代码创建一个JAR,并计划从另一个项目运行测试 下面是我试图实现的方法 项目B: configurations { testApi } task testJar .... artifacts { testApi testJar } 项目A: dependencies { testRuntime "xx.xxx.xxx.projectName", configuration: "integtest"; } 测试似乎没有使用这种方法运行。你知道会是什么问题吗?有更好的方法

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

下面是我试图实现的方法

项目B:

configurations { testApi }
task testJar ....
artifacts { testApi testJar }
项目A:

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

测试似乎没有使用这种方法运行。你知道会是什么问题吗?有更好的方法吗?

Gradle只在通过
testClassesDirs
属性配置的目录中查找测试类,该属性是
test
任务的
java
插件的
project.sourceset.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()
}

请再详细一点,e。G什么是“未运行”什么错误,等等。当我运行
gradletest
时,测试类无法从jar中识别。但是构建成功完成了Tanks@Vampire,我遇到了
初始化错误
java.lang.ClassNotFoundException
尝试上述方法时,我猜它无法识别类。有什么想法吗?我尝试解包JAR并设置
testClassesDir=files(“${buildDir}/integTest/classes/java”)
。这也导致了同样的错误。然后把它放在类路径上,就像你最初做的一样,我相应地更新了我的答案。似乎这也不起作用。任何关于使用JAR中提取的文件的建议,我都试图使用类似
testClassesDir=files(${buildDir}/integTest/classes/java”)
,这也会导致相同的错误。此外,不推荐使用
testClsssesDir
(不带尾随s),请确保
testJar
任务运行良好。该文件的结构是什么?包含类名的确切错误消息是什么?