ry1633
ry1633

Reputation: 463

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...

@Transactional
    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'
            return
        }
        if (faqCategoryInstance.hasErrors()) {
            respond faqCategoryInstance.errors, view:'create'
            return
        }
        if(!faqCategoryInstance.save(flush:true)){
            respond faqCategoryInstance.errors, view:'create'
            return
        }
        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:

@Test
    void "test save with null params"(){
        def cont = new FaqCategoryController()
        cont.request.method = 'GET'
        cont.params.id = ''
        cont.save() 
        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.*

-r

Upvotes: 2

Views: 26374

Answers (1)

Mario David
Mario David

Reputation: 1615

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

Upvotes: 1

Related Questions