1

I'm trying to use Springockito and spies to verify that calls were made/not made on a service method during an end-to-end test. I'm autowiring the service that the process will also get, and spy on it. Although myService instance is instrumented, verify() does not verify previous calls, but makes a call to the original method and passes a null parameter. Why is this?

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = PatchedSpringockitoContextLoader.class, locations = {
    "classpath:/config.xml"
})
...
@Autowired
@WrapWithSpy
private MyService myService;
...
@Before
public void setup() {
    initMocks(this);
    ...
}
...
@Test
public void test() {
    // run the process that may or may not call the service
    verify(myService, never()).myMethod(any(MyParam.class));
}
Laszlo B
  • 455
  • 3
  • 14

1 Answers1

1

What might be happening here is that your spied object uses annotations (e.g @Transactional) that requires Spring to add an AOP proxy around your spy, which causes Mockito to malfunction.

I've had the same issue as yours although I do not use Spock, and I solved it by getting a reference to the proxied mock or spy from the Spring proxy.

Check out the suggested hack in this GitHub issue report.

I am not using Spring Boot, so I wrapped the workaround code in a @BeforeClass method.

Anthony
  • 644
  • 7
  • 23