Spock测试与junit 5测试一起无法运行 [英] Spock test together with junit 5 test does not run

查看:348
本文介绍了Spock测试与junit 5测试一起无法运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的堆栈:

  • IDEA 2019.1.3
  • Springboot 2.1.6
  • Java 11
  • Maven 3.8.0
  • Groovy 2.5
  • Spock 1.3
  • 朱尼木星5.5.1
  • Junit vintage 5.5.1
  • GMavenPlus插件2.7.1

我们想开始在Spock测试框架中编写测试.我遵循了此方法,但是我没有成功.当我尝试运行所有测试时,我的Spock测试未运行.

We would like to start writing tests in Spock testing framework. I followed this howto, but I was not successful. My spock tests are not running when I try to run all of my tests.

我能够进行一项测试.我可以在测试上右键单击"并运行它.但是,如果我尝试运行整个groovy软件包(或Java软件包下的某些软件包),它将不会运行那些groovy测试.它不会运行意味着出现以下错误:

I am able to run one test. I can "right-click" on test and run it. But if I try to run whole groovy package (or some package under Java package) it will not run those groovy tests. It will not run means following error:

Jul 24, 2019 8:33:47 AM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-jupiter' failed to discover tests
java.lang.NoClassDefFoundError: org/junit/platform/engine/support/discovery/SelectorResolver
    at org.junit.jupiter.engine.JupiterTestEngine.discover(JupiterTestEngine.java:69)
    at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:168)
    at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:155)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.engine.support.discovery.SelectorResolver
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 8 more

Jul 24, 2019 8:33:47 AM org.junit.platform.launcher.core.DefaultLauncher handleThrowable
WARNING: TestEngine with ID 'junit-vintage' failed to discover tests
java.lang.NoClassDefFoundError: org/junit/platform/engine/support/discovery/SelectorResolver
    at org.junit.vintage.engine.VintageTestEngine.discover(VintageTestEngine.java:62)
    at org.junit.platform.launcher.core.DefaultLauncher.discoverEngineRoot(DefaultLauncher.java:168)
    at org.junit.platform.launcher.core.DefaultLauncher.discoverRoot(DefaultLauncher.java:155)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: org.junit.platform.engine.support.discovery.SelectorResolver
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    ... 8 more

有人可以解释为什么可能吗?

Can someone explain why is it possible?

Spock测试:

class TelephoneTest extends Specification {

    def "Should copy correct values from telephone buidler."() {

        given:
        TelephoneInfoType telephone = telephone().build().toOurType()

        expect:
        telephone.getCountry() == COUNTRY
        telephone.getNumber() == NUMBER
        telephone.getType() == TYPE
        telephone.getLocation() == LOCATION
    }
}

我的文件夹结构:

src/main/java
src/test/groovy
src/test/java

我希望看到测试正在运行,这意味着:我可以右键单击文件夹src/test/groovy,选择运行测试.

I would like to see tests are running which means: I can right-click folder src/test/groovy a select run tests.

推荐答案

这是我在IDEA中看到的内容:

This is what I see in IDEA:

因此,也许您想更具体一点,并共享一个 MCVE ,即一个完整的Maven项目,其中包含一些虚拟类和测试. (Spock和JUnit)都在GitHub上供我检查.

So maybe you want to get more specific and share an MCVE, i.e. a full Maven project with a few dummy classes and tests (both Spock and JUnit), on GitHub for me to inspect.

更新:在我的叉子中检查并修复了MCVE之后,我可以解释出什么问题了:

Update: After inspecting and fixing your MCVE in my fork I can explain what was wrong:

  1. 用于Spock测试的MCVE文件夹为'src/test/spock'.我将其重命名为"src/test/groovy",以使GMavenPlus能够找到它.这可以修复Groovy测试编译.

  1. Your MCVE folder for Spock tests was 'src/test/spock'. I renamed it to 'src/test/groovy' in order to enable GMavenPlus to find it. This fixes Groovy test compilation.

在您的POM中,您手动覆盖了三个JUnit Jupiter工件的依赖版本,但是mvn help:effective-pom向我显示,在您的版本为5.5.1时,还有一些仍在5.3.2上.我不知道为什么您除了希望拥有尖端技术并始终使用最新版本外,还需要更新它们.无论如何,有效的POM还表明您的父POM中存在以下与JUnit相关的版本属性:

In your POM you manually overrode the dependency versions for three JUnit Jupiter artifacts, but mvn help:effective-pom showed me that some others still were on 5.3.2 while your version was 5.5.1. I am not sure why you think you need to update them other than wishing to be bleeding edge and always use the latest version. Anyway, the effective POM also shows that there are these JUnit-related version properties in your parent POM:

<junit-jupiter.version>5.3.2</junit-jupiter.version>
<junit.version>4.12</junit.version>

更确切地说,这些属性来自

More exactly, those properties are from your parent POM's own parent POM. The rest was easy: Just override the relevant property in your own POM:

<properties>
  <junit-jupiter.version>5.5.1</junit-jupiter.version>
</properties>

现在运行mvn clean test并查看是否正在编译和运行JUnit和Spock测试.像我上面的屏幕快照一样,从IntelliJ IDEA运行测试现在也可以进行.

Now run mvn clean test and see that both JUnit and Spock tests are being compiled and run. Running the tests from IntelliJ IDEA like in my screenshot above also works now.

我还通过GitHub向您发送了拉动请求.

I also sent you a pull request via GitHub.

这篇关于Spock测试与junit 5测试一起无法运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆