2

In my current setup i want to unit test a Grails service that has an @autowired dependency and inject a mock for the dependency.

class AcmeService {

    @Autowired
    FooService fooService // not a Grails service!
}

The FooService is not a Grails service but it is a dynamic implementation from a FeignClient. I am looking for a way to inject a Mock for the FooService service in a UnitTest. What would be the best solution to do this?

I tried setting the dependency in the setup, but then i get a 'Unsatisfied dependency expressed through field fooService'

class AcmeService extends Specification {

    FooService mockedFooService = Mock(FooService)

    def setup() {
        service.fooService = mockedFooService
    }
}
Marco
  • 15,101
  • 33
  • 107
  • 174

1 Answers1

2

you can add the following to your unit test:

def doWithSpring = {
    fooService( InstanceFactoryBean, Mock(FooService) )
}
Graeme Rocher
  • 7,985
  • 2
  • 26
  • 37