mardi 16 décembre 2014

Grails webflow plugin integration test sample

It is difficult to find resources up to date about how to write Grails Webflow integration tests. You would lose precious time reading internet entries you would better invest in writing integration tests.

I am using Grails 2.4.3, the latest Webflow plugin and Spring security plugin which I want to test along with the flow.

package reportbyorigin

import grails.test.mixin.TestMixin
import grails.test.mixin.webflow.WebFlowUnitTestMixin

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.core.Authentication
import org.springframework.security.core.context.SecurityContextHolder

import spock.lang.*
import taackaccess.User

@TestMixin(WebFlowUnitTestMixin)
class AllControllerSpec extends GroovyTestCase {

    void setUp() {
        User admin = User.findByUsername("admin")
        Authentication auth =
          new UsernamePasswordAuthenticationToken(admin, null)
          SecurityContextHolder.context.authentication = auth
    }

    def cleanup() {
    }

    void "test flow user instance"() {
        mockController(AllController)
  
        when: "user login"
            statEntryFlow.putDatasOnFlow.action()

        then: "test flow values"
            flow.loggedUser instanceof User
    }
}

The tricky parts are "void setUp()" instead of "def setup()", "extends GroovyTestCase" instead of all you can read on the internet, "mockController(<yourcontroller>)".

Hope this sample will help someone.

Aucun commentaire:

Enregistrer un commentaire