JUnit5 ConsoleLauncher NoClassDefFoundError

cigdeys3  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(109)

我的测试文件使用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位于lib目录中,与JUnit ConsoleLauncher jar沿着。
$ 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

30byixjq

30byixjq1#

根据@Sormuras的评论,将特定的jar添加到类路径中是可行的。然而,如果使用了许多依赖项,这似乎不是一个理想的解决方案:
$ java -jar lib/junit-platform-console-standalone-1.3.2.jar -cp lib/commons-io-2.6.jar:out --scan-class-path

enyaitl3

enyaitl32#

我遇到了同样的问题,然后我把命令改为:

java -cp lib/*:out org.junit.platform.console.ConsoleLauncher --scan-class-path

lib文件夹包含junit-platform-console-standalone磁盘,它工作了。

相关问题