Kel Varnsen
Kel Varnsen

Reputation: 324

Running JUnit5 from Command line with dependencies

I have a Java project with the following files:
C:\MyProject\myPackage\MyTests.class
C:\MyProject\lib\junit-platform-console-standalone-1.5.2.jar
C:\MyProject\lib\other-library.jar

The MyTests file was compiled using an IDE with the junit-platform-console-standalone-1.5.2.jar and the other-library.jar. I verified that the tests compile and run successfully with the IDE, but my question is:
How do I run all the tests in MyTests.class from the command-line (Windows command prompt), including ones that depend on the other-library.jar file?

From the "MyProject" directory I've tried:

java -jar lib/junit-platform-console-standalone-1.5.2.jar -cp .;libs --scan-class-path --disable-ansi-colors

and this does run some of the test methods, but a ClassNotFoundException is thrown for any of the test methods that use the other-library.jar file. Does anyone know what I may be doing wrong?

I know if I compile it with a JUnit4 jar file then this command runs fine:

java -cp .;libs/* org.junit.runner.JUnitCore myPackage.MyTests

I'm essentially trying to do the same thing but with JUnit5.

Upvotes: 0

Views: 1280

Answers (1)

JockX
JockX

Reputation: 2126

Given the layout
C:\MyProject\myPackage\MyTests.class
C:\MyProject\lib\junit-platform-console-standalone-1.5.2.jar
C:\MyProject\lib\other-library.jar

This command should work. The jar is explicitly listed just as the package root directory

C:\MyProject> java -jar lib/junit-platform-console-standalone-1.5.2.jar   \
                   --class-path=.;lib\other-library.jar                   \
                   --scan-class-path

Alternatively, the java command may be run with -cp arguments, and Junit main method called directly. This should allow to load all the jars into classpath in a wildcard manner.
Strangely java -cp is not equivalent of junit -cp argument:

> java -cp ".;lib\*" org.junit.platform.console.ConsoleLauncher --scan-class-path

Upvotes: 1

Related Questions