yazabara
yazabara

Reputation: 1353

Grails 2.4.4: services does not inject: integration test

I have service with dependency service like:

class ParserService {
    def depService;

    private def parseLine(lineParts) {
    ...
    def set = depService.findItemByName(tmpModule.name);//depService == null
    ...

I try to implement integration test like:

@TestFor(ParserService)
class ParserServiceTest extends IntegrationSpec {
    def "should not parse comment"() {
        when:
        ...
        def resultList = service.parseAnnotations(inputStream);

resources.groovy:

beans = {
}

And I have NullPointerException: depService - null

Upvotes: 0

Views: 762

Answers (2)

albertovilches
albertovilches

Reputation: 350

The @TestFor annotation is only for unit tests. The integration tests work like a normal beans, so Grails/Spring will inject your dependencies in your tests if you define the services as properties in your class, like in a controller/service/domain class.

Upvotes: 1

defectus
defectus

Reputation: 1987

Your test case should extend the class GroovyTestCase or IntegrationSpec and that's pretty much it. Of course the dependency injection then works the usual way

class ParserServiceTest extends IntegrationSpec {
   ParserService parserService

   def "should not parse comment"() {
    when:
    ...
    def resultList = parserService.parseAnnotations(inputStream)
...

Upvotes: 1

Related Questions