使用命令行运行带有依赖关系的JUnit5

huangapple go评论90阅读模式
英文:

Running JUnit5 from Command line with dependencies

问题

我有一个Java项目,其中包含以下文件:
C:\MyProject\myPackage\MyTests.class
C:\MyProject\lib\junit-platform-console-standalone-1.5.2.jar
C:\MyProject\lib\other-library.jar

MyTests文件是使用带有junit-platform-console-standalone-1.5.2.jar和other-library.jar的集成开发环境(IDE)编译的。我验证了在IDE中这些测试编译并成功运行,但我的问题是:
我如何从命令行(Windows命令提示符)运行MyTests.class中的所有测试,包括依赖于other-library.jar文件的测试?

从“MyProject”目录下,我尝试了以下命令:

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

这个命令确实运行了一些测试方法,但是对于任何使用other-library.jar文件的测试方法,都会抛出ClassNotFoundException异常。有人知道我可能做错了什么吗?

我知道如果我使用JUnit4的jar文件进行编译,那么下面这个命令可以正常运行:

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

实质上,我正在尝试以类似的方式使用JUnit5来完成相同的操作。

英文:

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.

答案1

得分: 1

给定以下布局:
C:\MyProject\myPackage\MyTests.class
C:\MyProject\lib\junit-platform-console-standalone-1.5.2.jar
C:\MyProject\lib\other-library.jar

此命令应该有效。该jar文件明确列在包根目录之后

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

或者,也可以使用 -cp 参数运行 java 命令,并直接调用 Junit 的主方法。这应该允许以通配符方式将所有的jar文件加载到类路径中。奇怪的是,java -cp 不等同于 junit 的 -cp 参数:

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

英文:

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

huangapple
  • 本文由 发表于 2020年10月10日 06:50:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64288252.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定