简体   繁体   中英

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

I wrote a simple feature method with the following then block :

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

But the gradle test fails with the error :

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 ?

I had a similar problem trying to write a spring-boot test.

Do not use @RunWith(SpringRunner.class) and try removing your junit runner if you're using another one.

For me my Spock Specification executed properly without specifying the SpringRunner.

I found that the issue in my case is that the test file/class ended with Test , rather than Spec . Changing that resolved the issue (eg SomeServiceTest -> SomeServiceSpec ).

I had the same problem. For me worked moving the test file from "src" directory to "Test" directory.

Try adding the following property to your build.gradle

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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