4
votes

In Grails 2.1.x, I would like to create an additional dataSource for an integration test so that I can verify that my service is dynamically pulling data from a user specified dataSource. Currently my tests are pretty simple such as:

@Test
void "get Action Types by data source name returns all action types"() {
    new ActionCache(actionType: 'Action Type 1').test.save()
    new ActionCache(actionType: 'Action Type 2').test.save()
    new ActionCache(actionType: 'Action Type 3').save()

    def result = reportService.getActionTypesByDataSource('test')
    assert result.size() == 2
}

I can get the test to pass if I configure a new dataSource for the test environment in DataSource.groovy named test, but then the new dataSource shows up in all of my tests; unit and integration. Ideally I would like to create a new dataSource as part of the setUp for the integration test using something like:

def grailsApplication

@Before
void setUp() {
    grailsApplication.config.dataSource_test = {
        dbCreate = "update"
        url = "jdbc:h2:mem:testDb;MVCC=TRUE"
    }
}

But it appears that the dataSources are loaded prior to the integration tests being run and I can't figure out how to add to them.

1
some of the plugins here fbflex.wordpress.com/2012/12/29/testing-grails may be of use. - Ray Tayek
That's a useful list, but there's nothing on there that answers my question. - Justin S

1 Answers

0
votes

It sounds like a custom environment may be the way to go. In DataSource.groovy you should see a section that looks something like this:

test {
    dataSource {
        dbCreate = "update"
        url = "jdbc:h2:mem:testDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
    }
}

I would add a custom environment called integrationTest just after the test block like so:

integrationTest {
    dataSource {
        dbCreate = "update"
        url = "jdbc:h2:mem:myinttestDb;MVCC=TRUE;LOCK_TIMEOUT=10000"
    }
}

To run with this custom environment you would start the grails app like this:

grails -Dgrails.env=integrationTest run-app

Hope this helps.