【发布时间】:2019-01-28 20:23:05
【问题描述】:
我的测试文件使用 Commons IO 库:
import org.apache.commons.io.FileUtils;
...
@Test
public void testExampleEvalOutput_dataset() throws IOException {
File file1 = FileUtils.getFile(pathToProject + pathToExampleResults + "exampleEvalOutput_dataset.csv");
File file2 = FileUtils.getFile(pathToProject + pathToYourResults + "exampleEvalOutput_dataset.csv");
assertEquals(true, FileUtils.contentEquals(file1, file2));
}
当我从 Eclipse 运行时,测试工作正常。
尝试从命令行运行。编译测试文件:
$ mkdir out
Commons IO jar 与 JUnit ConsoleLauncher jar 一起位于 lib 目录中。
$ javac -d out -cp "lib/*" path/to/ExampleEvalTests.java
编译作品。
但是,库在运行时没有被拾取:
$ java -jar lib/junit-platform-console-standalone-1.3.2.jar -cp lib:out/ --scan-class-path
JUnit Vintage:ExampleEvalTests:testExampleEvalOutput_dataset
MethodSource [className = 'org.company.test.ExampleEvalTests', methodName = 'testExampleEvalOutput_dataset', methodParameterTypes = '']
=> java.lang.NoClassDefFoundError: org/apache/commons/io/FileUtils
org.company.test.ExampleEvalTests.testExampleEvalOutput_dataset(ExampleEvalTests.java:27)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.base/java.lang.reflect.Method.invoke(Method.java:564)
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
[...]
目录结构:
lib
org
company
test
out
如何让out 中的可执行文件获取lib?
【问题讨论】:
-
请枚举位于“lib”目录中的所有 JAR 文件。喜欢:
java -jar ... -cp lib/apache-commons-x.jar:another-tool.jar:out --scan-class-path -
@Sormuras 成功了,谢谢!
-
@Sormuras 通配符 jar 可以使用 -cp 参数加载到 java 类路径,如下所示:
java -cp ".;lib\*" org.junit.platform.console.ConsoleLauncher --scan-class-path。不知道为什么 junit 的-cp不能以相同的方式工作。
标签: java junit command-line classpath junit5