Grails 3.3.8:无法解析ControllerUnitTest

时间:2018-12-13 23:46:57

标签: grails

我正在尝试编写一些基本的控制器测试,并且正在遵循文档https://docs.grails.org/latest/guide/testing.html。我的测试当前无法找到 ControllerUnitTest 。我缺少依赖项吗?

这是我的进口

grails.testing.web.controllers.ControllerUnitTest

1 个答案:

答案 0 :(得分:1)

查看位于https://github.com/jeffbrown/mcroteaucontrollertest/blob/master/src/test/groovy/mcroteaucontrollertest/DemoControllerSpec.groovy的项目。

https://github.com/jeffbrown/mcroteaucontrollertest/blob/cf5f09b1c2efaba759f6513301d7b55fcb29979b/build.gradle#L56

package mcroteaucontrollertest

import grails.testing.web.controllers.ControllerUnitTest
import spock.lang.Specification

import static org.springframework.http.HttpStatus.METHOD_NOT_ALLOWED

class DemoControllerSpec extends Specification implements ControllerUnitTest<DemoController> {

    void "test GET request"() {
        when:
        controller.index()

        then:
        response.text == 'Success'
    }

    void "test POST request"() {
        when:
        request.method = 'POST'
        controller.index()

        then:
        response.status == METHOD_NOT_ALLOWED.value()
    }
}

ControllerUnitTest特性由grails-web-testing-support提供。确保您有testCompile "org.grails:grails-web-testing-support"之类的内容,如{{3}}所示。