【问题标题】:unable to run junit5 tests from terminal using Junit5 Console Launcher无法使用 Junit5 控制台启动器从终端运行 junit5 测试
【发布时间】:2020-01-08 09:31:57
【问题描述】:

我正在为我的 Spring Boot 应用程序执行单元测试。测试工作正常,并在从 intellij 运行时给出预期的输出。我正在尝试使用 Junit5 Console Launcher 从终端运行相同的测试。 这是我使用的命令:-

java -jar junit-platform-console-standalone-1.6.0-RC1.jar --class-path . -c AdapterTest

我正在从out/tests/package that contains the test class 文件夹运行上述命令。

我可以在我的外部 jars 文件夹中看到所需的 jars 和依赖项。但是当我从终端运行它时出现以下错误。

整个堆栈跟踪:-

java -jar junit-platform-console-standalone-1.6.0-RC1.jar --class-path . -c AdapterTest
Thanks for using JUnit! Support its development at https://junit.org/sponsoring
org.junit.platform.commons.JUnitException: TestEngine with ID 'junit-jupiter' failed to discover tests
        at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:189)
        at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:168)
        at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132)
        at org.junit.platform.console.tasks.ConsoleTestExecutor.executeTests(ConsoleTestExecutor.java:66)
        at org.junit.platform.console.tasks.ConsoleTestExecutor.lambda$execute$0(ConsoleTestExecutor.java:58)
        at org.junit.platform.console.tasks.CustomContextClassLoaderExecutor.replaceThreadContextClassLoaderAndInvoke(CustomContextClassLoaderExecutor.java:41)
        at org.junit.platform.console.tasks.CustomContextClassLoaderExecutor.invoke(CustomContextClassLoaderExecutor.java:31)
        at org.junit.platform.console.tasks.ConsoleTestExecutor.execute(ConsoleTestExecutor.java:58)
        at org.junit.platform.console.ConsoleLauncher.executeTests(ConsoleLauncher.java:95)
        at org.junit.platform.console.ConsoleLauncher.execute(ConsoleLauncher.java:73)
        at org.junit.platform.console.ConsoleLauncher.execute(ConsoleLauncher.java:50)
        at org.junit.platform.console.ConsoleLauncher.execute(ConsoleLauncher.java:43)
        at org.junit.platform.console.ConsoleLauncher.main(ConsoleLauncher.java:37)
Caused by: org.junit.platform.commons.JUnitException: ClassSelector [className = 'AdapterTest'] resolution failed
        at org.junit.platform.launcher.listeners.discovery.AbortOnFailureLauncherDiscoveryListener.selectorProcessed(AbortOnFailureLauncherDiscoveryListener.java:39)
        at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolveCompletely(EngineDiscoveryRequestResolution.java:102)
        at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.run(EngineDiscoveryRequestResolution.java:82)
        at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolver.resolve(EngineDiscoveryRequestResolver.java:113)
        at org.junit.jupiter.engine.discovery.DiscoverySelectorResolver.resolveSelectors(DiscoverySelectorResolver.java:45)
        at org.junit.jupiter.engine.JupiterTestEngine.discover(JupiterTestEngine.java:69)
        at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:181)
        ... 12 more
Caused by: org.junit.platform.commons.PreconditionViolationException: Could not load class with name: AdapterTest
        at org.junit.platform.engine.discovery.ClassSelector.lambda$getJavaClass$0(ClassSelector.java:75)
        at org.junit.platform.commons.function.Try$Failure.getOrThrow(Try.java:335)
        at org.junit.platform.engine.discovery.ClassSelector.getJavaClass(ClassSelector.java:74)
        at org.junit.jupiter.engine.discovery.ClassSelectorResolver.resolve(ClassSelectorResolver.java:66)
        at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.lambda$resolve$2(EngineDiscoveryRequestResolution.java:134)
        at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
        at java.util.ArrayList$ArrayListSpliterator.tryAdvance(ArrayList.java:1359)
        at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
        at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:499)
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:486)
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
        at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
        at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:531)
        at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolve(EngineDiscoveryRequestResolution.java:185)
        at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolve(EngineDiscoveryRequestResolution.java:125)
        at org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolution.resolveCompletely(EngineDiscoveryRequestResolution.java:91)
        ... 17 more
Caused by: java.lang.ClassNotFoundException: AdapterTest
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
        at java.net.FactoryURLClassLoader.loadClass(URLClassLoader.java:817)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
        at org.junit.platform.commons.util.ReflectionUtils.lambda$tryToLoadClass$9(ReflectionUtils.java:790)
        at org.junit.platform.commons.function.Try.lambda$call$0(Try.java:57)
        at org.junit.platform.commons.function.Try.of(Try.java:93)
        at org.junit.platform.commons.function.Try.call(Try.java:57)
        at org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass(ReflectionUtils.java:753)
        at org.junit.platform.commons.util.ReflectionUtils.tryToLoadClass(ReflectionUtils.java:709)
        ... 32 more

我不确定为什么会收到此错误。尝试了这里提到的所有内容:- https://stackoverflow.com/questions/45869932/unable-to-run-tests-with-junit5-console-launcher 但我仍然收到上述错误。如何从终端运行测试?

更新:- 尝试从 out/tests 运行命令:- 得到以下输出:-

Thanks for using JUnit! Support its development at https://junit.org/sponsoring
╷
├─ JUnit Jupiter ✔
└─ JUnit Vintage ✔
Test run finished after 24 ms
[         2 containers found      ]
[         0 containers skipped    ]
[         2 containers started    ]
[         0 containers aborted    ]
[         2 containers successful ]
[         0 containers failed     ]
[         0 tests found           ]
[         0 tests skipped         ]
[         0 tests started         ]
[         0 tests aborted         ]
[         0 tests successful      ]
[         0 tests failed          ]

测试仍未运行。

【问题讨论】:

  • 您是否尝试过从ˋout/testsˋ 运行命令?类路径组件应指向编译目标目录的根目录。

标签: java spring-boot gradle junit5


【解决方案1】:

这是适用于我从项目的根文件夹运行它的命令:

java -jar junit-platform-console-standalone-1.5.2.jar -classpath ./bin --scan-classpath

./bin 是包含我编译的类的目标文件夹。

我使用这个命令从终端编译我的测试:

javac -cp './src/:junit-jupiter-api-5.6.0-20200102.170103-207.jar' -d ./bin ./src/Assignment1Test.java

./src/Assignment1Test.java 是我的测试类。

所以,我从这个文件夹结构开始:

project
 |
 |-- junit-jupiter-api-5.6.0-20200102.170103-207.jar
 |-- junit-platform-console-standalone-1.5.2.jar
 |--bin
 |--src
     \ Assignment1.java
     \ Assignment1Test.java

我最终得到了这个:


project
 |
 |-- junit-jupiter-api-5.6.0-20200102.170103-207.jar
 |-- junit-platform-console-standalone-1.5.2.jar
 |--bin
     \ Assignment1.class
     \ Assignment1Test.class
 |--src
     \ Assignment1.java
     \ Assignment1Test.java

然后我运行我的测试

【讨论】:

  • 我试过你说的。它无法找到在我的测试类中导入的其他类和库。
  • 那么,问题是在你编译你的测试类的时候出现的?您需要编译源文件夹中的每个文件,包括您正在测试的类,应该首先编译。根据您使用的测试引擎,您需要使用引擎的 jar 编译测试类。对于 Junit5,你得到了 junit-jupiter-api,我正在使用的那个,而为了向后兼容,你得到了老式的。如果您仍然遇到问题,请尝试链接 repo 或其他内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-08
  • 2018-08-12
  • 1970-01-01
  • 2019-11-01
相关资源
最近更新 更多