Grails, spock technologies

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* SignUpControllerSpec.groovy */
@TestMixin(WebFlowUnitTestMixin)
@TestFor(SignUpController)
class SignUpControllerSpec extends Specification {

    def "test empty signup form submission"() {

        when: "user loads page with registration form"
            registerFlow.start.action()

        then: "empty userCommand object is created and lastEvent is start"
            lastEventName == 'start'
            flow.userCommand instanceof UserCommand

        when: "user submits the form with empty lines"
            registerFlow.signUp.on.submit.action()

        then: "userCommand with errors is returned"
            flow.userCommand.hasErrors()
            lastEventName == 'signUp'
            lastTransitionName == 'submit'
    }
}

/* SignUpController.groovy */
class SignUpController {

    def userService

    def registerFlow = {
        start {
            action {
                flow.userCommand = new UserCommand()
            }
            on('success').to('signUp')
        }

        signUp {
            on('submit') {
                bindData(flow.userCommand, params)
                def cmd = flow.userCommand
                if (!cmd.validate()) {
                    return error()
                }

                // TODO: process valid userCommand

            }.to('end')
        }

        end {
            redirect(uri: '/')
        }
    }
}

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

Have fun!

Looking to hire a software engineer?
Don't hesitate to contact us.

Comments