Monday, May 13, 2013

Grails Goodness: Testing Views and Templates

Grails has great support for testing. We can unit test controllers, taglibs, services and much more. One of the things we can unit test are views and templates. Suppose we have a view or template with some (hopefully simple) logic (a view/template should not contain complex logic), for example an if-else statement to display some content conditionally. We can write a test in Grails to see if the logic is correctly implemented.

Let's first create a template in our Grails application:

%{-- File: /grails-app/views/sample/_header.gsp --}%
<g:if test="${username}">
    <h1>Hi, ${username}</h1>

The template checks if there is a username attribute and shows the value is there is, otherwise a simple "Welcome" message is shown.

We can test this with the following Spock specification. We use Spock for writing the test, because it makes writing tests so much easier and more fun! We must make sure we use the GroovyPageUnitTestMixin, because this will add a render() method to our tests. The render() method accepts a Map arguments where we can set the template or view and optionally the model with attributes.

// File: test/unit/com/mrhaki/grails/views/HeaderTemplateSpecification.groovy
package com.mrhaki.grails.views

import grails.test.mixin.TestMixin
import grails.test.mixin.web.GroovyPageUnitTestMixin
import spock.lang.Specification

class HeaderTemplateSpecification extends Specification {

    def "if username is set then show message Hi with username"() {
        expect: 'Template output must contain Hi, mrhaki'
        renderTemplateWithModel([username: 'mrhaki']).contains 'Hi, mrhaki'

    def "if username is not set then show message Welcome"() {
        expect: 'Template output must contain Welcome'
        renderTemplateWithModel().contains 'Welcome'

    private renderTemplateWithModel(model = [:]) {
        render(template: '/sample/header', model: model)


Let's also write a simple Grails view GSP, which shows a list of items if items are set or otherwise show a message using the <g:message/> tag.

%{--File: grails-app/views/sample/items.gsp--}%
<%@ page contentType="text/html;charset=UTF-8" %>
<head><title>Simple GSP page</title></head>
<g:if test="${items}">
        <g:each in="${items}" var="item">
    <g:message code="noitems"/>

The following specification will check the output dependent on if there are elements in the model attribute items. Because we use GroovyPageUnitTestMixin we have access to a messageSource object where we can define values for keys that are used by the <g:message/> tag.

// File: test/unit/com/mrhaki/grails/views/ItemsViewSpecification.groovy
package com.mrhaki.grails.views

import grails.test.mixin.TestMixin
import grails.test.mixin.web.GroovyPageUnitTestMixin
import spock.lang.Specification

class ItemsViewSpecification extends Specification {

    def "if items are available then show items as list items"() {
        final String output = renderViewWithItems(['one'])

        output.contains '
  • one
  • ' } def "if collection of items is empty then show message items are empty"() { given: messageSource.addMessage 'noitems', request.locale, 'NO_ITEMS' when: final String output = renderViewWithItems() then: output.contains 'NO_ITEMS' } private renderViewWithItems(items = []) { render(view: '/sample/items', model: [items: items]) } }

    Code written with Grails 2.2.1


    Simon T. said...

    In the second example the gsp claims to be '/sample/items', but in the test you render '/sample/user'. I guess they should be the same?

    Hubert Klein Ikkink said...

    @Simon T. : Yes you're right. I have changed the sample code, because they should be the same.

    √úrgo Ringo said...

    For some reason when I try this sample then message don't get resolved. Instead I get content that has message keys. Do you have any idea why this happens? I use Grails 2.3.2

    Post a Comment