Search code examples
grails

Grails 3.3.8 : Unable to resolve ControllerUnitTest


I am attempting to write some basic controller tests and am following the documentation https://docs.grails.org/latest/guide/testing.html. My test currently is unable to find ControllerUnitTest. Is there a dependency that I am missing?

Here is my import

grails.testing.web.controllers.ControllerUnitTest

Solution

  • See the project at https://github.com/jeffbrown/mcroteaucontrollertest.

    https://github.com/jeffbrown/mcroteaucontrollertest/blob/master/src/test/groovy/mcroteaucontrollertest/DemoControllerSpec.groovy

    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()
        }
    }
    

    The ControllerUnitTest trait is provided by grails-web-testing-support. Make sure you have something like testCompile "org.grails:grails-web-testing-support" as shown at https://github.com/jeffbrown/mcroteaucontrollertest/blob/cf5f09b1c2efaba759f6513301d7b55fcb29979b/build.gradle#L56.