Skip to content

Commit

Permalink
fix for GRAILS-8248 "Unit test withFilters not working for grails 2.0…
Browse files Browse the repository at this point in the history
….0.RC1"

Filter unit testing support was swallowing exceptions giving the impression that it was not working, when there were in fact problems in the user code
  • Loading branch information
graemerocher committed Nov 9, 2011
1 parent 804b865 commit fa87b29
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Expand Up @@ -131,6 +131,7 @@ class FiltersUnitTestMixin extends ControllerUnitTestMixin {
}
} catch (e) {
interceptor.afterCompletion(request, response, this, e)
throw e
}
}
}
@@ -0,0 +1,70 @@
package org.codehaus.groovy.grails.web.filters

import javax.servlet.http.HttpServletResponse
import grails.artefact.Artefact
import grails.test.mixin.TestFor
import grails.test.mixin.Mock
import spock.lang.Specification

/**
* Created by IntelliJ IDEA.
* User: graemerocher
* Date: 11/9/11
* Time: 3:48 PM
* To change this template use File | Settings | File Templates.
*/
@TestFor(UserController)
@Mock(AuthenticationFilters)
class FiltersUnitTestSpec extends Specification{
void "test filters are applied for a unit test"() {
when:"A filter is used around a controller"
params.username = ''
withFilters(action: "create") {
controller.create()

}
then:"Check that the filter logic is applied"
400 == response.status
}

void "test filters relay exceptions"() {
when:"A filter is used around a controller"
params.username = ''
withFilters(action: "update") {
controller.update()
}
then:"Check that an exception is rethrown"
RuntimeException e = thrown()
e.message == "bad things happened"
}

}

@Artefact("Controller")
class UserController {

def create() {}
def update() {}
}

@Artefact("Filters")
class AuthenticationFilters {
def filters = {
create(controller: 'user', action: 'create') {
before = {
if (params.username == '') {
render(status: HttpServletResponse.SC_BAD_REQUEST)
return false
}
}
}
create(controller: 'user', action: 'update') {
before = {
if (params.username == '') {
throw new RuntimeException("bad things happened")
return false
}
}
}
}
}

0 comments on commit fa87b29

Please sign in to comment.