Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails spock tests with private methods failing after upgrading to 2.5.0

Tags:

grails

spock

We've upgraded our app from Grails 2.4.4 to 2.5.0. All are working fine, but some test cases are failing which containing some private methods.

import org.junit.Before

class UserServiceSpec extends IntegrationSpec {

    @Before
    def setup() {
         // Some initialization
    }

    // being used in other test methods
    private void getParams() {
          // some code
    }

    void testUserUniqueEmali() {
         when: "this runs"
         // code
         then: "hello"
         // code
    }
}

Exception thrown:

java.lang.IllegalAccessException: Class org.spockframework.util.ReflectionUtil can not access a member of class com.test.UserServiceSpec with modifiers "private"
    at org.spockframework.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:153)
    at org.spockframework.runtime.model.MethodInfo.invoke(MethodInfo.java:84)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
    at _GrailsTest.run_closure1(_GrailsTest.groovy:102)
    at TestApp.run_closure1(TestApp.groovy:32)

I generated dependency report and here are some stats:

org.spockframework:spock-core:1.0-groovy-2.4
org.codehaus.groovy:groovy-all:2.4.3

This shows that latest version of the Spock framework is being used, but I'm not able to fix this problem. I've tried removing "private" modifier, but it still not working.

like image 812
Shashank Agrawal Avatar asked Apr 16 '15 17:04

Shashank Agrawal


2 Answers

After investigating for some time, I figured out the problem but not the root cause.

Basically, that @Before annotation was the culprit and due to some reason, spock test was throwing that error. Removing that unused annotation fixed my problem.

This was working in Grails 2.4.4 and starts failing in 2.5.0, might be because of changes in Spock framework.

like image 62
Shashank Agrawal Avatar answered Nov 07 '22 22:11

Shashank Agrawal


I've encountered the same problem. To prevent them just don't name methods annotated with JUnit's @Before or @After like Spock's methods: cleanup and setup.

like image 7
ZZ 5 Avatar answered Nov 07 '22 23:11

ZZ 5