Getting Error Error executing script TestApp: java.lang.ClassNotFoundException: grails.plugin.spock.test.GrailsSpecTestType when trying to run test

1,262 views
Skip to first unread message

paulo...@gmail.com

unread,
Oct 3, 2013, 11:42:23 AM10/3/13
to spockfr...@googlegroups.com
Hi everyone,

I'm a newbie in Groovy, Grails and Spock. I have a domain class called EntityContact and in it a function initialize. Inside that function i am calling a static method to get the id of the current user. So i want to mock of that static method so that it returns a predefined value.
I am using Grails 2.2.1, Groovy 2.0.4, Spock 0.7 in IntelliJ IDEA 12.1.4. And when i try to run the test it prints:  Error executing script TestApp: java.lang.ClassNotFoundException: grails.plugin.spock.test.GrailsSpecTestType when trying to run test.
I have checked the plugins of my app and only one uses spock and it doesn't export that dependency and even if it did it uses the same version as i am. Any ideas about how to solve the problem?

  void initialize() {
        isMain = false
        creationDate = new Date()
        createdBy = Person.get(SecurityUtil.retrieveCurrentUser(false))        <-the static method i want to mock
    }


@TestFor(EntityContact)
class EntityContactTests extends Specification{



    def "test Entity Contact with Person and SecurityUtil mocks"() {

        setup:
            GroovyMock(Person, global: true)
            GroovyMock(SecurityUtil, global: true)
            SecurityUtil.retrieveCurrentUser(false).id >> 1
            Person.get(1) >> new CernPerson(firstName: "Johnl", lastName: "Doe")

        when: "initializing an EntityContact"
            def contact = new EntityContact()
            contact.initialize()

        then: "the new EntityContact "
            assert entity.validate()
    }

}

Thanks!

Steinar Haugen

unread,
Oct 3, 2013, 1:23:23 PM10/3/13
to spockfr...@googlegroups.com
You can achieve this by manipulating the metaClass. Something like this:

// replace your mock of SecurityUtil with this
SecurityUtil.metaClass.'static'.retrieveCurrentUser = { boolean value -> 1 }

// add a clean up section in your test method
cleanup:
SecurityUtil.metaClass.'static'.retrieveCurrentUser = null

Regards,
Steinar


--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spockframewor...@googlegroups.com.
To post to this group, send email to spockfr...@googlegroups.com.
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/groups/opt_out.

paulo...@gmail.com

unread,
Oct 3, 2013, 3:44:25 PM10/3/13
to spockfr...@googlegroups.com
I tried your code but it still produces the same error when i try to run the test. It compiles fine and then when i try to run the unit test it produces this CLassNotFoundException. It seems that there is this class GrailsSpecTestType that it can't fnd. Maybe there is a problem with the way Grails fetches the dependency. I'll try tomorrow to add it manually to the project classpath and see what happens. I saw a post somewhere that this problem appeared in another guy's project when he upgraded Groovy to 2.0 from 1.8.7 and the spock plugin from 0.6 to 0.7. Do you know of any other similar cases?

On a sidenote to you know of any tutorials i can checkout for testing in Grails?

Thanks.

Steinar Haugen

unread,
Oct 3, 2013, 5:09:51 PM10/3/13
to spockfr...@googlegroups.com
The more I think about it, the more I realize that my suggestion wasn't very productive since your original code using GroovyMock does pretty much the same as what I suggested, but in a cleaner way. 

Now that you mention it, I too seem to recall somebody having this ClassNotFoundException problem, but since I hadn't run across it myself I didn't really take a notice of it. A quick google search turned out at least one interesting link, though:

http://jira.grails.org/browse/GRAILS-10144 : In this case it was the gwt plugin that included an older version of Spock. The solution was to exclude Spock as a transient dependency:
compile(':gwt:0.8') {
    exclude 'spock'
}

So you might have a look at the transient dependencies of your plugins and 3rd party libraries and verify whether the correct version of Spock is actually used.

Best regards,
Steinar

paulo...@gmail.com

unread,
Oct 4, 2013, 7:50:28 AM10/4/13
to spockfr...@googlegroups.com
I fixed the problem. It had to do with one of the plugins also using the spock dependency but there was an error in the way the dependency was called, so i fixed the code and now it doesn't produce any ClassNotFoundException.

Thanks for the help anyway!
Reply all
Reply to author
Forward
0 new messages