How to create sample data for Grails integration test?

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

@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 pass the test if I configure a new data source for the test environment in DataSource.groovy called test , but then a new data source appears in all my tests. unit and integration. Ideally, I would like to create a new data source as part of setUp for an 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 seems that dataSources are loading before running the integration tests, and I cannot figure out how to add them.

+4
source share
1 answer

It seems like a normal environment can be a way. 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 user environment called integrationTest right after the test block:

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

To run this custom environment, you must start the grails application as follows:

 grails -Dgrails.env=integrationTest run-app 

Hope this helps.

0
source

All Articles