Search code examples
javajunitcommand-lineclasspathjunit5

JUnit5 ConsoleLauncher NoClassDefFoundError


My test file uses Commons IO library:

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));
}

Tests work when I run from Eclipse.

Trying to run from command line. Compile test file:

$ mkdir out

Commons IO jar is in lib directory, along with JUnit ConsoleLauncher jar.

$ javac -d out -cp "lib/*" path/to/ExampleEvalTests.java

Compiling works.

However, the library is not picked up at runtime:

$ 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)
   [...]

Directory structure:

lib org company test out

How do I get the executable in out to pick up lib?


Solution

  • Based on @Sormuras comment, adding the specific jar used to the classpath works. However, this doesn't seem like an ideal solution if many dependencies are used:

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