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.
The problem is caused by a def name = {
closure in the controller. This seems to interfere with the AST transformation taking place. Changing the closure to a method, ie def name() {
allows the tests to run.
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.