@Mock/@InjectMocks for groovy - spock

Viewed 5770

In JUnit / Mockito we have 2 extremly useful annotations: @Mock and @InjectMocks.

In my new project i started using groovy with spock for testing, I'm wondering if there is a replacement for mentioned annotations?

2 Answers

Someone wrote an annotation two months ago: https://github.com/msid256/MockInjector4Spock.

The bean you want to test doesn't need to be instantiated manually. All you need to do is to declare it as a field and annotate it with @InjectMocks.

@Service
class ServiceC {
    @Autowired
    public ServiceC(ServiceA a, ServiceB b) {}
}

class DemoSpec extends Specification {
    @Autowired
    ServiceA serviceA;

    ServiceB serviceB = Mock(ServiceB.class)

    @InjectMocks // from MockInjector4Spock - de.github.spock.ext.annotation.InjectMocks
    ServiceC serviceC;
}
Related