简体   繁体   中英

Grails 2.2.2: Testing interceptors in ControllerTests with GMock

When our codebase was still in Grails 2.1.4, this unit test for our interceptors worked with full coverage:

@Before
void setUp() {
   gmc = new GMockController()
   apiProducts = gmc.mock()

   api.products.returns(apiProducts).stub()

   controller.api = api
}

void testBeforeInterceptor() {
   // Arrange
   setUp()
   def ctrl = gmc.mock(controller)
   params.id = 1
   ctrl.actionName.returns('view')
   apiProducts.get(params.id)

   // Act
   gmc.play {
       controller.beforeInterceptor()
   }
}

After switching to Grails 2.2.2, it seems like this doesn't cover the test like it used to in 2.1.4. The error "Unexpected method call" is being called out since it won't go through the interceptors like it used to in 2.1.4.

Was there a change in how interceptors are tested?

I've searched around and it seems like no one has the answer for this. The documentation on testing interceptors are sparse. Any ideas?

Grails does not invoke interceptors or servlet filters when calling actions during integration testing. You should test interceptors and filters in isolation, using functional testing if necessary.

(Untested) I am not sure whether this is applicable here for GMock. But integration tests doc mentions otherwise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM