Sunday, February 15, 2009

Grails Integration Testing: common information

Integration tests differ from unit tests in that you have full access to the Grails environment within the test. Grails will use an in-memory HSQLDB database (or another configured data source, for instance MySQL) for integration tests and clear out all the data from the database in between each test. You have to understand that each test is wrapped into transaction which will be rolled back lately. So integration test:
  • will talk to database, so it will be slower than unit test
  • will persist data to database - make sure Your test environment database setting don't point to production or development database
A couple of helpful thoughts concerning integration tests:
  1. You have to initialize the service class yourself, there is no dynamic injection. It is the same way as it was during unit testing.
  2. If the service has other service class inside it, You have to initialize and assign it manually. Also it is the same way as it was during unit testing.
  3. Sometimes it is useful to flush session in order to persist to database immediately - else You might get weird thing like no "id" for the supposedly saved domain object or deleted objects would be not deleted. How to do that? It's very easy, please look at the simple example:
    class DocumentServiceTests extends GroovyTestCase {

    def documentService

    // Dynamic injection of session factory.
    def sessionFactory

    void setUp() {

    // You have to initialize service,
    // there is no dynamic injection.

    documentService = new DocumentService()

    void testSomething() {

    // Some logic. For instance,
    // documentService.deleteDocumentById(1L).
    // But document is still not
    // deleted after execution.
    // You have to manually flush the session
    // before assertions.


    // Assertions.


That's all for now.

1 comment:

  1. Hello,
    The Article on Grails Integration Testing is informative. It gives detailed information about it.Thanks for Sharing the information on Integration Testing .For More information check the detail on Integration testing check, Software Testing Services