Grails 2.5.5 controller unit test cannot cast object error

By Doug Palmer

I’ve been trying to set up a unit test for a controller. However, any attempt to access the controller property causes the following error:

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'class au.org.ala.collectory.ContactController' with class 'java.lang.Class' to class 'au.org.ala.collectory.ContactController'

Here’s the code that I’ve been using:

package au.org.ala.collectory
import grails.test.mixin.Mock
import grails.test.mixin.TestFor
import spock.lang.Specification
@TestFor(ContactController)
@Mock([Contact, CollectoryAuthService])
class ContactControllerTests extends Specification {
    def contact
    def setup() {
        contact = new Contact(
            title: "Dr",
            firstName: "Lemmy",
            lastName: "Caution",
            phone: "0262465909",
            mobile: "0419468551",
            email: "lemmy.caution@csiro.au",
            notes: "to be treated with exaggerated respect",
            publish: true,
            userLastModified: 'test')
     }
    def testList() {
        when:
        contact.save(flush:true, failOnError: true)
        request.contentType = JSON_CONTENT_TYPE
        controller.list()
        then:
        model.contactInstanceList.size() == 1
        model.contactInstanceList[0].title == 'Dr'
    }
}

I’ve tried various combinations of code placement and mocking, all to no avail. Any explanation as to why I would be getting this error would be most appreciated.

Source: Stack Overflow

    

Share it with your friends!

    Fatal error: Uncaught Exception: 12: REST API is deprecated for versions v2.1 and higher (12) thrown in /home/content/19/9652219/html/wp-content/plugins/seo-facebook-comments/facebook/base_facebook.php on line 1273