ry1633 ry1633 - 4 years ago 279
Groovy Question

groovy.lang.MissingPropertyException: No such property: CREATED for class:

I am walking through integration tests on the controller below, it is a generic project to create a category for FAQs on a web probject. And the following test returns a "groovy.lang.MissingPropertyException: No such property: CREATED for class: "

Controller snippet:

static allowedMethods = [
index: 'GET', show: 'GET', create: 'GET', edit: 'GET',
save: 'POST', update: 'PUT', delete:'DELETE'

...more stuff...

def save(FaqCategory faqCategoryInstance) {
if (faqCategoryInstance == null) {
flash.errorMessage = message(code: 'default.not.found.message', args: [message(code: 'faqCategory.label', default: 'FAQ Category'), params.id])
redirect action: 'index', method: 'GET'
if (faqCategoryInstance.hasErrors()) {
respond faqCategoryInstance.errors, view:'create'
respond faqCategoryInstance.errors, view:'create'
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.created.message', args: [message(code: 'faqCategory.label', default: 'FAQ Category'), faqCategoryInstance.id])
redirect faqCategoryInstance
'*' { respond faqCategoryInstance, [status: CREATED] }

Test snippet:

void "test save with null params"(){
def cont = new FaqCategoryController()
cont.request.method = 'GET'
cont.params.id = ''
assertEquals 'FAQ Category not found with id', cont.flash.errorMessage
assertEquals "/faqCategory/index", cont.response.redirectUrl

The ironic thing is that I have the (almost) the exact same test in a very similar controller, and that particular test doesn't throw any errors at all and the import statements are identical in each file. I understand the error msg, but I don't understand where it's coming from or how to fix it.

Here also are the import statements that are common to both test files that I alluded to above:

import static org.junit.Assert.*
import ....DbunitGroovyTestCase // can't show path because of organizational security
import junit.framework.JUnit4TestAdapter
import grails.test.mixin.TestFor
import junit.framework.TestCase

import org.junit.*
import spock.lang.*


Answer Source

You should use the following import statement: import static org.springframework.http.HttpStatus or just use the plain error code: respond faqCategoryInstance, [status: 201]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download