• Skip to primary navigation
  • Skip to main content
  • Skip to footer

JH Tech Services

A knowledgebase

  • Portfolio
  • Knowledgebase
  • About

Add data to Functional tests in bootstrap.groovy but different data for Integration tests

November 7, 2014 by jer0dh

Using Grails 2.4.3 with Eclipse

Developing an app that adds data to the in memory h2 database using the bootstrap.groovy. I had developed some functional tests using this data and later found myself developing some Integration tests as well. In order to test all the scenarios, I needed to alter the data in bootstrap.groovy substantially which would affect all my functional tests. So instead, I wanted to load separate data in the setup() function of my integration tests and leave my bootstrap data alone, but the bootstrap data interfered with my integration data. You can add conditionals in the bootstrap like so


import grails.util.Environment
class BootStrap {

    def init = { servletContext ->
		
		if (Environment.current == Environment.TEST ) {
                .....

But I only wanted to load my functional data if I was running a functional test. This method would load it for both my functional and integration tests.

I found this blog by Andrew Taylor that gave me a solution
http://www.redtoad.ca/ataylor/2011/02/setting-grails-functional-test-database/

So using his events.groovy script file and altering my conditional in bootstrap, I was able to achieve a solution.

In Grails Project\scripts folder
Events.groovy


eventTestPhaseStart = { args ->
	System.properties["grails.test.phase"] = args
}

In Grails Project\conf\BootStrap.groovy


import grails.util.Environment

class BootStrap {

    def init = { servletContext ->
		if (Environment.current != Environment.TEST || System.properties['grails.test.phase'] == "functional") {
                    def adminUser = new User(username:"admin", password: "pass").save(failOnError:true)
		    def userUser = new User(username:"joe", password: "pass").save(failOnError:true)
                   ...

So now my data would be loaded only during run-app or when running a functional test

  • Grails

Footer

  • Portfolio
  • Knowledgebase
  • About

© 2023