groovy.lang.MissingPropertyException:没有这样的属性:CREATED for class:

时间:2014-07-23 16:11:33

标签: grails groovy junit classnotfoundexception

我正在下面的控制器上进行集成测试,它是一个通用项目,用于在webproject上创建FAQ的类别。以下测试返回" groovy.lang.MissingPropertyException:没有这样的属性:为类创建:"

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
    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
讽刺的是,我在一个非常相似的控制器中进行了(几乎)完全相同的测试,并且该特定测试根本不会抛出任何错误,并且每个文件中的import语句都是相同的。我理解错误消息,但我不知道它来自何处或如何解决它。

以下是我在上面提到的两个测试文件共有的import语句:

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

1 个答案:

答案 0 :(得分:1)

您应该使用以下import语句: import static org.springframework.http.HttpStatus或只使用普通错误代码:respond faqCategoryInstance, [status: 201]