0
votes

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

2
Do you have a service called DepService? make sure your spelling is correct - dsharew
I have depService. I can run application(not test) - and all works fine. - yazabara
Also try to post more code, how are you calling the service(using the service) - dsharew

2 Answers

1
votes

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.

1
votes

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)
...