Skip to content

Instantly share code, notes, and snippets.

@johnrengelman
Created April 10, 2013 14:46
Show Gist options
  • Save johnrengelman/5355266 to your computer and use it in GitHub Desktop.
Save johnrengelman/5355266 to your computer and use it in GitHub Desktop.
Example Grails Filter Unit Test
@TestFor(FooController)
@Mock(FooFilters)
class FooSpec extends Specification {
FooService fooServiceMock
def "testing a filter"() {
given:
defineBeans {
fooService(MethodInvokingFactoryBean) {
targetObject = this
targetMethod = 'mockFooService'
}
}
FiltersGralisPlugin.reloadFilters(grailsApplication, applicationContext)
when:
withFilters(action: 'list') {
controller.list()
}
then:
1 * fooService.bar() >> true
}
FooService mockFooService() {
fooServiceMock = Mock(FooService)
return fooServiceMock
}
}
class FooService {
boolean bar() { return true
}
class FooFilters {
def fooService
def filters = {
list(controller: 'foo', action: 'list') {
before = {
return fooService.bar()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment