0
votes

I am using grails 4 to create unit test for controller https://testing.grails.org/latest/guide/index.html

My controller is under grails-app/controllers/mypack/myfolder/exampleController.groovy

My unit test is under src/test/groovy/mypack/myfolder/exampleControllerSpec.groovy

my unit test is like this class exampleControllerSpec extends Specification implements ControllerUnitTest< exampleController>

but it complain can not resolve symbol 'exampleController'

anything wrong here?

how to import exampleController

1
Which symbol cannot be resolved, and do you have import statements for all of the classes/traits/interfaces mentioned by name in your source file? - Jeff Scott Brown
can not resolve symbol 'exampleController', looks like it can not find the controller groovy, how can I import the groovy controller? - lee
"how can I import the groovy controller" - Controllers are imported just like any other class. However, in your case since the test and the controller are in the same package an import statement should not be needed. Is the first line of code in both grails-app/controllers/mypack/myfolder/exampleController.groovy and src/test/groovy/mypack/myfolder/exampleControllerSpec.groovy look like package mypack.myfolder? Separate from that, class names that begin with a lower case letter (like exampleController and exampleControllerSpec) are considered a violations of best practice. - Jeff Scott Brown
yes, both start package same: package package-path , controller is under grails-app/controllers/package-path/, unit test is under src/test/groovy/package-path/ - lee
The original question mentions src/test/groovy/mypack/myfolder/exampleControllerSpec.groovy and also mentions DemoControllerSpec. Have you defined a class named DemoControllerSpec in a file named exampleControllerSpec.groovy? - Jeff Scott Brown

1 Answers

0
votes

The project at https://github.com/jeffbrown/leecontrollertest demonstrates how to construct the test.

https://github.com/jeffbrown/leecontrollertest/blob/d6fc272a69406f71286bf5268794ef4a4252a15b/grails-app/controllers/mypack/myfolder/exampleController.groovy

package mypack.myfolder

// NOTE: The nonstandard class naming convention here is intentional
// per https://stackguides.com/questions/65723769/grails-4-unit-test-controller-could-not-find-class
class exampleController {

    def hello() {
        render 'Hello, World!'
    }
}

https://github.com/jeffbrown/leecontrollertest/blob/d6fc272a69406f71286bf5268794ef4a4252a15b/src/test/groovy/mypack/myfolder/exampleControllerSpec.groovy

package mypack.myfolder

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

// NOTE: The nonstandard class naming convention here is intentional
// per https://stackguides.com/questions/65723769/grails-4-unit-test-controller-could-not-find-class
class exampleControllerSpec extends Specification implements ControllerUnitTest<exampleController> {

    void "test action which renders text"() {
        when:
        controller.hello()

        then:
        status == 200
        response.text == 'Hello, World!'
    }
}