The problem is that grails-testing newbies like me will want to try to do unit testing for services. In short, don't do that unless your service classes are simple and easy. (Keep this in mind when you start your project -- you won't see this unless you've experienced the pain before).
The best way that I found so far is to make service tests into integration tests. This mean putting service tests into the "integration" folder under the test directory. For more explanation on the difference between Unit Testing and Integration Testing in Grails, go here.
Testing Service classes is easy once you understand that it is:
- it is an integration test -- will talk to the database
- it is slow -- probably because of the data connection overhead
- will persist data to database -- make sure your test environment database settings don't point to a production database
class FooServiceTests extends GroovyTestCase {A couple of helpful points as indicated by the numbers above:
def fooService
void setUp() {
fooService = new FooService // *1
fooService.otherService = new OtherService() // *2
}
void testServiceCall() {
def user = new User(name:"Test", email:"test@name.com")
user.save(flush:true) // *3
fooService.doSomething(user) // *4
assertEquals user.result, "expected result"
}
}
- You have to initialize the service class yourself, there is no DI.
- If the service has other service classes inside it, initialize and assign it manually
- Remember to always flush to persist to the database immediately -- else you might get weird things like no "id" for the supposedly saved domain object.
- In integration mode, fooService will act like a "headless" Grails app
I hope this is helpful to you -- I may not be right, so feedback and corrections are welcomed.
2 comments:
Might try the testing-plugin or use a beta version of Grails 1.1 which has this plugin integrated, because it pretty much mocks the data access for you without relying on slow integration tests.
This means you can do fast unit testing with your service classes.
Testing plugin was one of the thing I tried for unit testing -- not a bed or roses.
I'm still looking for more effective ways of testing service as unit test.
Post a Comment