Skip to content

Mockito fails to verify mocked @Service when service contains @Retryable methods #6828

Closed
@pearj

Description

@pearj

I'm using spring-boot 1.4.0.FINAL and I'm having trouble when I have a mocked @Service which has @Retryable annotations in the class. When I call Mockito.verify() I get an UnfinishedVerificationException exception:

12:05:36.554 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - After test method: context [DefaultTestContext@5ec0a365 testClass = MockBatchTestWithRetryVerificationFailures, testInstance = sample.batch.MockBatchTestWithRetryVerificationFailures@5abca1e0, testMethod = batchTest@MockBatchTestWithRetryVerificationFailures, testException = org.mockito.exceptions.misusing.UnfinishedVerificationException: 
Missing method call for verify(mock) here:
-> at sample.batch.service.MyRetryService$$FastClassBySpringCGLIB$$7573ce2a.invoke(<generated>)

Example of correct verification:
    verify(mock).doSomething()

This looks very similar to #5837, in fact, I managed to hack to workaround based off that issue:

See: sample.batch.MockBatchTestWithRetryVerificationFailuresWorkaround.batchTest()

@Test
public void batchTest() throws Exception {
    service.process();

    if (service instanceof Advised) {
        service = (MyRetryService) ((Advised) service).getTargetSource().getTarget();
    }

    verify(service).process();
    validateMockitoUsage();
}

I have put an example project together here that reproduces this issue: https://github.com/pearj/spring-boot-batch-retry-issue
For the failure case see: sample.batch.MockBatchTestWithRetryVerificationFailures.batchTest()

Is it possible for a transparent fix somehow?

I also have this on stackoverflow

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions