For demo purposes I have set up a fresh grails application with these files:
class HalloController {
def index() {
String heading = request.getAttribute("heading")
render "${heading}"
}
}
class HalloInterceptor {
boolean before() {
request.setAttribute("heading", "halloechen") // *** set breakpoint here***
true
}
boolean after() { true }
void afterView() {
// no-op
}
}
When I got to http://localhost:8080/hallo "halloechen" gets printed as this was set as an request attribute in the interceptors before()
method, just like I wanted it to be.
Now I want to have a unit test for the interceptor:
class HalloInterceptorSpec extends Specification implements InterceptorUnitTest<HalloInterceptor> {
def setup() {
}
def cleanup() {
}
void "Test hallo interceptor matching"() {
when:"A request matches the interceptor"
withRequest(controller:"hallo")
then:"The interceptor does match"
interceptor.doesMatch() && request.getAttribute("heading") == "halloechen"
}
}
This test fails as the heading
attribute does not get set to the request (which is a mocked request anyway). In fact, when running the unit test it seems as the interceptor doesn't even get called. I have set a breakpoint in the before()
method and when debugging the test I never get there. Which is odd because I would expect a Interceptor test to call the interceptor at least.
I know I could rewrite the test as described here but my point is that the interceptor doesn't get called at all.
Is that right? Another thing: calling getModel()
in the test alway returns null
. How do I get the model in my test?