简体   繁体   English

带有闭包的 Spock 单元测试以错误“没有可运行的方法”结束

[英]Spock Unit test with a closure ends with error "No runnable methods"

I wrote a simple feature method with the following then block :我用以下then块编写了一个简单的功能方法:

then:
1 * view.setAttachments({ it?.size == 3 })

But the gradle test fails with the error :但是gradle test失败并显示错误:

initializationError

java.lang.Exception: No runnable methods
    at org.junit.runners.BlockJUnit4ClassRunner.validateInstanceMethods(BlockJUnit4ClassRunner.java:169)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:104)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:355)
    ...

initializationError

java.lang.Exception: Test class should have exactly one public zero-argument constructor
    at org.junit.runners.BlockJUnit4ClassRunner.validateZeroArgConstructor(BlockJUnit4ClassRunner.java:147)
    at org.junit.runners.BlockJUnit4ClassRunner.validateConstructor(BlockJUnit4ClassRunner.java:124)
    at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:103)
    at org.junit.runners.ParentRunner.validate(ParentRunner.java:355)
    at org.junit.runners.ParentRunner.<init>(ParentRunner.java:76)
    ...

I noticed that if I remove the closure, there is no more error我注意到如果我删除了闭包,就不会再有错误了

then:
1 * view.setAttachments(_) // everything works well without closure!

It seems Spock tries to run the closure like it was another test method... How to avoid that ?似乎 Spock 试图像运行另一种测试方法一样运行闭包……如何避免这种情况?

I had a similar problem trying to write a spring-boot test.我在尝试编写 spring-boot 测试时遇到了类似的问题。

Do not use @RunWith(SpringRunner.class) and try removing your junit runner if you're using another one.不要使用 @RunWith(SpringRunner.class) 并尝试删除您的junit runner,如果您正在使用另一个。

For me my Spock Specification executed properly without specifying the SpringRunner.对我来说,我的 Spock 规范在没有指定 SpringRunner 的情况下正确执行。

I found that the issue in my case is that the test file/class ended with Test , rather than Spec .我发现我的问题是测试文件/类以Test结束,而不是Spec Changing that resolved the issue (eg SomeServiceTest -> SomeServiceSpec ).更改解决了问题(例如SomeServiceTest -> SomeServiceSpec )。

I had the same problem.我有同样的问题。 For me worked moving the test file from "src" directory to "Test" directory.对我来说,将测试文件从“src”目录移动到“Test”目录。

Try adding the following property to your build.gradle尝试将以下属性添加到您的 build.gradle

 test {
        systemProperty 'project.buildDir', project.buildDir
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM