Search code examples
unit-testinggrailsintegration-testingeasyb

How can I clear the database (domains) between easyb scenarios in Grails Integration Testing?


I am running an Integration Test for a Grails application. I am using the easyb plugin. The problem is that the database doesn't seem to get cleared out between Scenarios. My When I run standard Grails Integration Tests, the persistence context is cleared between each test. The easyb Stories are in the Integration folder, but the Grails Integration Test rules don't seem to apply here... So how do you make easyb clean up after itself?

P.S. I'm defining multiple scenarios in the same groovy file fwiw, but I don't think this is necessarily pertinent.


Solution

  • Just incase somebody like me is still dealing with this issue and looking for a way to rollback after each test scenario, below is a solution that works (thanks to Burt Beckwith's blog).

    Wrap each easyb test scenario in a with transaction block and manually rollback at the end

    scenario "add person should be successful", {
    Person.withTransaction { status -> 
        given "no people in database", {
        }
        when "I add a person", {
            Person.build()
        }
        then "the number of people in database is one", {
            Person.list().size().shouldEqual 1
        }
        status.setRollbackOnly()
    }
    }
    
    scenario "database rollback should be successful", {
    given "the previous test created a person", {
    }
    when "queried for people", {
        people = Person.list().size()
    }
    then "the number of people should be zero", {
        people.shouldEqual 0
    }
    }
    

    The above test passes. Please post if you have a better solution to the problem