Я перепробовал почти все ссылки и посты, доступные по этой теме, и у меня полно идей.У меня есть мой класс
public interface IncomingRecordProcessor {
@Retryable(value = IOException.class,
maxAttemptsExpression = "#{${message.retry.max-attempts:5}}",
backoff = @Backoff(
delayExpression = "#{${message.retry.delay:500}}",
maxDelayExpression = "#{${message.retry.max-delay:5000}}",
multiplierExpression = "#{${message.retry.multiplier:2.0}}"
)
)
void process(List<IncomingRecordDto> messages) throws IOException;
@Recover
void recover(IOException e, List<IncomingRecordDto> messages);
}
Мой тестовый класс:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@ActiveProfiles("retry")
public class SpringTest {
@Autowired
private IncomingRecordProcessor incomingRecordProcessor;
@Before
public void setUp() {
System.setProperty("spring.profiles.active", "retry");
}
@After
public void validate() {
validateMockitoUsage();
}
@Test
public void retriesAfterOneFailAndThenPass() throws Exception {
incomingRecordProcessor.process(getMessages());
verify(incomingRecordProcessor, times(4)).process(any());
}
@Configuration
@EnableRetry
@EnableAspectJAutoProxy(proxyTargetClass=true)
@Profile("retry")
public static class Application {
@Bean
public DefaultIncomingRecordProcessor incomingRecordProcessor() throws IOException {
ElasticSearchService elasticSearchService = mock(ElasticSearchService.class);
ElasticSearchConfigProperties props = mock(ElasticSearchConfigProperties.class);
FailedRecordProcessor failedRecordProcessor = mock(FailedRecordProcessor.class);
AuditRecordBuilder auditRecordBuilder = mock(AuditRecordBuilder.class);
when(elasticSearchService.bulkIndex(anyList(), any()))
.thenThrow(IOException.class).thenReturn(mock(BulkResponse.class));
return Mockito.spy(new DefaultIncomingRecordProcessor(
elasticSearchService,
props,
failedRecordProcessor,
auditRecordBuilder)
);
}
}
private List<IncomingRecordDto> getMessages() {
IncomingRecordDto rec1 = new IncomingRecordDto("k1", "v1".getBytes());
IncomingRecordDto rec2 = new IncomingRecordDto("k2", "v2".getBytes());
IncomingRecordDto rec3 = new IncomingRecordDto("k3", "v3".getBytes());
List<IncomingRecordDto> recList = new ArrayList<>();
recList.add(rec1);
recList.add(rec2);
recList.add(rec3);
return recList;
}
}
}
Я получаю только эту ошибку:
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is of type IncomingRecordProcessor$DefaultIncomingRecordProcessor$$EnhancerBySpringCGLIB$$e81597f8 and is not a mock!
Make sure you place the parenthesis correctly!
See the examples of correct verifications:
verify(mock).someMethod();
verify(mock, times(10)).someMethod();
verify(mock, atLeastOnce()).someMethod();
Iпоиграл без EnableAspectJAutoProxy
, но затем получил другую ошибку:
org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is of type $Proxy54 and is not a mock!
Make sure you place the parenthesis correctly!
Любая помощь приветствуется.