Details
Description
Following the instructions for unit test for filters on http://grails.org/doc/2.0.0.RC1/guide/testing.html#unitTestingFilters
However, the withFilters closure is never called when running the test. Below is the code:
Filter class:
class AuthenticationFilters {
def filters = {
create(controller:'openAm', action:'create') {
before = {
if (params.username == '') {
render (status: HttpServletResponse.SC_BAD_REQUEST)
return false
}
}
}
Controller unit test
@TestMixin(GrailsUnitTestMixin)
@TestFor(OpenAmController)
@Mock (AuthenticationFilters)
class OpenAmControllerTests {
@Test
void testCreateActionIsFiltered() {
withFilters(action: "create") {
println 'hello'
controller.params.username = ''
controller.create()
}
assert 400 == response.status
}
}
-
Hide
- filter-test.zip
- 09/Nov/11 7:23 AM
- 22 kB
- Graeme Rocher
-
- grails-app/.../ApplicationResources.groovy 0.1 kB
- grails-app/conf/BootStrap.groovy 0.1 kB
- grails-app/conf/BuildConfig.groovy 2 kB
- grails-app/conf/Config.groovy 4 kB
- grails-app/conf/DataSource.groovy 1 kB
- grails-app/conf/UrlMappings.groovy 0.2 kB
- grails-app/.../AuthenticationFilters.groovy 0.4 kB
- grails-app/conf/spring/resources.groovy 0.0 kB
- grails-app/.../UserController.groovy 0.1 kB
- grails-app/i18n/messages.properties 3 kB
- grails-app/.../messages_cs_CZ.properties 3 kB
- grails-app/i18n/messages_da.properties 3 kB
- grails-app/i18n/messages_de.properties 4 kB
- grails-app/i18n/messages_es.properties 3 kB
- grails-app/i18n/messages_fr.properties 2 kB
- grails-app/i18n/messages_it.properties 2 kB
- grails-app/i18n/messages_ja.properties 4 kB
- grails-app/i18n/messages_nl.properties 3 kB
- grails-app/.../messages_pt_BR.properties 3 kB
- grails-app/.../messages_pt_PT.properties 3 kB
- grails-app/i18n/messages_ru.properties 4 kB
- grails-app/i18n/messages_sv.properties 3 kB
- grails-app/i18n/messages_th.properties 6 kB
- grails-app/.../messages_zh_CN.properties 2 kB
- grails-app/views/error.gsp 0.3 kB
- grails-app/views/index.gsp 3 kB
- grails-app/views/layouts/main.gsp 2 kB
- test/.../AuthenticationFiltersTests.groovy 0.3 kB
- test/unit/.../UserControllerTests.groovy 0.3 kB
- application.properties 0.1 kB
Activity
- All
- Comments
- Work Log
- History
- Activity
- Git Commits
Hide
Graeme Rocher
added a comment -
The actual issue was that the filter testing support was swallowing exceptions. So if you had some problems in your code you would not see the exception.
But as you can see with the attached application if your code doesn't produce an exception then the withFilters method works as expected.
The exception swallowing problem has now been fixed with the commit:
https://github.com/grails/grails-core/commit/fa87b29fab986fda521f6e77e7162bbb4c4ebf3f
Show
Graeme Rocher
added a comment - The actual issue was that the filter testing support was swallowing exceptions. So if you had some problems in your code you would not see the exception.
But as you can see with the attached application if your code doesn't produce an exception then the withFilters method works as expected.
The exception swallowing problem has now been fixed with the commit:
https://github.com/grails/grails-core/commit/fa87b29fab986fda521f6e77e7162bbb4c4ebf3f
I attached an application demonstrating the filter testing is working in 2.0 RC1