【问题标题】:JUnit5 ConsoleLauncher NoClassDefFoundErrorJUnit5 ConsoleLauncher NoClassDefFoundError
【发布时间】: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


【解决方案1】:

基于@Sormuras 评论,将用于类路径的特定 jar 添加到工作中。但是,如果使用许多依赖项,这似乎不是一个理想的解决方案:

$ java -jar lib/junit-platform-console-standalone-1.3.2.jar -cp lib/commons-io-2.6.jar:out --scan-class-path

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 2017-01-14
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 2013-10-19
    相关资源
    最近更新 更多