spock - How can I test my Grails service save method? -
i've created grails service using grails create-service taskcrudservice
. command created files taskcrudservice.groovy
in grails-app\services
, taskcrudservicespec.groovy
in test\unit
folder.
the method in taskcrudservice
is
task save(string name) { task task = new task() task.name = name task.save(flush: true, failonerror: true) }
i wanted test method so, i've filled body of "test something" method generated grails in test class. taskcrudservicespec
code:
@testfor(taskcrudservice) class taskcrudservicespec extends specification { void "test something"() { when: task t = service.save('task name') then: assert t.name == 'task name' } }
but test doesn't work. when run grails test-app
java.lang.illegalstateexception: method on class [task] used outside of grails application. if running in context of test using mocking api or bootstrap grails correctly. @ taskcrudservice.$tt__save(taskcrudservice.groovy:29) @ taskcrudservicespec.test create(taskcrudservicespec.groovy:24)
that looks should work, if did, it's bad test. never use unit tests testing persistence. use integration test you're using real database , not in-memory gorm implementation uses concurrenthashmap store data.
Comments
Post a Comment