Unit testing web flow in Grails

I’ve faced a problem with unit testing web flow controllers

There’s an existings WebFlowUnitTestMixin for unit testing webflow in GDK, but no word on how to use it in the latest Grails 2.2.0 official documentation.

I've used spock framework for my web flow controller unit test. Here is a pretty simple example:

/* SignUpControllerSpec.groovy */<br>@TestMixin(WebFlowUnitTestMixin)<br>@TestFor(SignUpController)<br>class SignUpControllerSpec extends Specification {<br><br>    def "test empty signup form submission"() {<br><br>        when: "user loads page with registration form"<br>            registerFlow.start.action()<br><br>        then: "empty userCommand object is created and lastEvent is start"<br>            lastEventName == 'start'<br>            flow.userCommand instanceof UserCommand<br><br>        when: "user submits the form with empty lines"<br>            registerFlow.signUp.on.submit.action()<br><br>        then: "userCommand with errors is returned"<br>            flow.userCommand.hasErrors()<br>            lastEventName == 'signUp'<br>            lastTransitionName == 'submit'<br>    }<br>}<br><br>/* SignUpController.groovy */<br>class SignUpController {<br><br>    def userService<br><br>    def registerFlow = {<br>        start {<br>            action {<br>                flow.userCommand = new UserCommand()<br>            }<br>            on('success').to('signUp')<br>        }<br><br>        signUp {<br>            on('submit') {<br>                bindData(flow.userCommand, params)<br>                def cmd = flow.userCommand<br>                if (!cmd.validate()) {<br>                    return error()<br>                }<br><br>                // TODO: process valid userCommand<br><br>            }.to('end')<br>        }<br><br>        end {<br>            redirect(uri: '/')<br>        }<br>    }<br>}

All of the default unit test controller properties are available + flow, conversation, lastTransitionName, lastEventName, currentEvent.

Have fun!

Software Developer