In a previous blog post we learned how we can unit test a template or view independently. But what if we want to unit test a controller that uses the
render() method and a template with the
template key instead of a view? Normally the view and model are stored in the
modelAndView property of the response. We can even use shortcuts in our test code like
model to check the result. But a
render() method invocation with a
template key will simply execute the template (also in test code) and the result is put in the response. With the
text property of the response we can check the result.
In the following sample controller we use the
header template and pass a
username model property to render output.