Emmanuel John
Emmanuel John

Reputation: 2325

IllegalStateException Running Grails Services Unit Tests

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

Upvotes: 3

Views: 362

Answers (1)

James Kleeh
James Kleeh

Reputation: 12228

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.

Upvotes: 8

Related Questions