Skip to content

Instantly share code, notes, and snippets.

@ck1125
Created June 9, 2012 17:37
Show Gist options
  • Save ck1125/2901905 to your computer and use it in GitHub Desktop.
Save ck1125/2901905 to your computer and use it in GitHub Desktop.
Filters with dependencies mocking with Spock
package grails_filter_tests
import spock.lang.Specification
import org.springframework.beans.factory.config.MethodInvokingFactoryBean
import grails.test.mixin.*
import grails.test.mixin.support.GrailsUnitTestMixin
import grails.test.mixin.web.FiltersUnitTestMixin
@TestFor(SimpleController)
@TestMixin([GrailsUnitTestMixin,FiltersUnitTestMixin])
@Mock(MySimpleFilters)
class MySimpleFiltersSpec extends Specification {
def 'show working mock test for filter'() {
given:
defineBeans {
simpleService(MethodInvokingFactoryBean) {
targetObject = this
targetMethod = "mockSimpleService"
arguments = ['hi']
}
}
when:
withFilters(action:"index") {
controller.index()
}
then:
request.text == 'hi'
response.text == 'hi'
}
SimpleService mockSimpleService(String text) {
SimpleService service = Mock(SimpleService)
service.aMethod() >> text
0 * service._
return service
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment