IllegalStateException Running Grails Services Unit Tests

340 views Asked by At

I have a simple grails service:

@Transactional
class SearchService {
    def doSomething() {
        10
    }
}

with a simple unit test:

class SearchServiceSpec extends Specification implements ServiceUnitTest<SearchService>{

    def setup() {
    }

    def cleanup() {
    }

    void "test something"() {
        expect:
        service.doSomething() == 10
    }
}

When I run the test, I get the following exception:

enter image description here

Anyone know what this means?

Strange thing is it works if I change doSomething to getSomething and then do service.something.

I have following versions: | Grails Version: 3.3.0 | Groovy Version: 2.4.11 | JVM Version: 1.8.0_60

1

There are 1 answers

3
James Kleeh On BEST ANSWER

This is due to the fact that the getter method is not having transactional behavior applied to it. To test a transactional method, you need to have a GORM implementation setup. The easiest way to do that for your test is to implement grails.testing.gorm.DataTest.