I have a Spring Boot application and Service with private DAO field inside it. Private DAO property is annotated with @Autowired
(no setters or constructors set it, just annotation).
I tried to write Spock test for service, but can't find how to inject mock DAO into @Autowired
variable.
class TestService extends Specification {
DAO dao = Mock(DAO)
Service service = new Service()
def "test save"() {
when:
service.save('data')
then:
1 * dao.save('data')
}
}
Any ideas?
UPD: I'm testing java code.
As result I did this:
One point was to use reflection. But Groovy can set private fields directly without additional manipulations. It was news for me.