Unfortunately, it may be a limitation in Grails 3 that you cannot use integration tests to test controllers.
For integration test controllers, it is recommended that you use create-functional-test to create a Geb functional test.
Source from Grails documentation
This seems to be a major change in direction from previous versions of grails. If you really need to test the controller in an integration test, you can try to do this:
NOTE. . I understand that this can be bad practice, and this goes against the Grails documentation, but sometimes you also need to test things more programmatically when unit tests are not enough and these Geb tests are not granular enough.
@TestFor(TestController) // This will provide a mocked "controller" reference @Integration @Rollback class TestControllerSpec extends Specification { // If TestController uses any services, have them autowired into this test @Autowired SomeService someService def setupSpec() { // Now connect those services to the controller controller.someService = someService } void "test something"() { when: controller.index() then: response.contentType != null } }
WARNING: After some additional work with this format, I found a problem. Using @TestFor will call Holders.clear() when it is completed, which means that there will be no grailsApplication object in grailsApplication . This will cause problems if you have any integration tests that run after one that uses the above approach. After multiple digging, it doesn't seem like there is a simple (or even tough) way to do this work, and maybe for some reason this is not supported in Grails 3. Note that one of the options is to mark other integration tests using @TestFor , so that the Holders class will be correctly populated. Is it a hack? Yes it! . You will need to decide whether to apply this overhead for all tests. In my case, it was just one other integration test that needed it (since it is a small application), but if it were more, I would not use this approach.
source share