Micronaut/GORM Unit Tests

I’ve recently revisited Micronaut as I need to re-write some old systems at work. Both of the systems are written in Grails, one provides an API, the other a very lightweight website. So Micronaut seems like the idea candidate. As a Grails developer I want to continue to use Groovy and GORM and both of these are supported.

I made a start on a few basic domain (Entity) classes with a few simple constraints. At this point I wanted to add some tests – yep, test driven development! However I ran in to problems getting the tests to run.

I created a test file following the same pattern as I would do in my Grails projects, but with what felt like common sense adaptations for Hibernate.

package uk.co.channele.mngorm

import io.micronaut.test.annotation.MicronautTest
import spock.lang.Specification

@MicronautTest
class AreaSpec extends Specification {

    void "test constraints"() {
        when: "an empty Area"
        def area = new Area()

        then: "not valid"
        !area.validate()

        when: "set the name"
        area.name = "name"

        then: "valid"
        area.validate()
    }
}

But when I tried to run the test with ./gradlew test I just got an exception:

org.springframework.dao.DataAccessResourceFailureException: Could not obtain current Hibernate Session; 
nested exception is org.hibernate.HibernateException: No Session found for current thread

This didn’t make sense, unit tests in Grails don’t expect the system to be running, mock objects are used instead. After a few late nights of searching/reading/cursing and trying many different variations I found this website https://guides.grails.org/grails-gorm-data-services/guide/index.html (notice no mention of Micronaut!) which gave me this solution:

package uk.co.channele.mngorm

import grails.gorm.transactions.Transactional
import io.micronaut.test.annotation.MicronautTest
import org.grails.orm.hibernate.HibernateDatastore
import org.springframework.transaction.PlatformTransactionManager
import spock.lang.AutoCleanup
import spock.lang.Shared
import spock.lang.Specification

@MicronautTest
@Transactional
class AreaSpec extends Specification {

    @Shared
    @AutoCleanup
    HibernateDatastore hibernateDatastore

    @Shared
    PlatformTransactionManager transactionManager

    void setupSpec() {
        hibernateDatastore = new HibernateDatastore(Area)
        transactionManager = hibernateDatastore.getTransactionManager()
    }

    void "test constraints"() {
        when: "an empty Area"
        def area = new Area()

        then: "not valid"
        !area.validate()

        when: "set the name"
        area.name = "name"

        then: "valid"
        area.validate()
    }
}

Which runs nicely and passes. Now I need to find a way to improve the Micronaut documentation so that potential users don’t suffer the same frustration as me!