Created
April 10, 2013 14:46
-
-
Save johnrengelman/5355266 to your computer and use it in GitHub Desktop.
Example Grails Filter Unit Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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