I am handling transactions in Spring boot 2.7 manually:
dataHeader = transactionTemplate.execute(status -> {
final var doc = findDoc(document);
return saveReport(doc);
});
private Doc findDoc(DocumentDto document) {
return docRepository.findByDocType(document.getType(), document.getId()), document.getReport()).get(0);
}
I found this answer hot to test such case, so I wrote this:
when(docRepository.findByDocType(any(), any(), any())).thenReturn(List.of());
when(transactionTemplate.execute(any(TransactionCallback.class)))
.then(invocation -> ((TransactionCallback<ReportDocMapping>) invocation.getArgument(0))
.doInTransaction(any(TransactionStatus.class)));
var log = LoggerWithMemoryFactory.getBufferedLoggerApi(DocService.class);
final var actual = logEvent.getMessage().getStringPayload();
if (!actual.contains(expected)) {
var message = String.format("expected: '%s' in '%s'", expected, actual);
throw new AssertionFailedError(message, expected, actual);
}
AFAIK I am not mixing any() matchers with values (like “X” or 10L). But still my test fails on wrong number of matchers for docRepository.findByDocType
.
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers! 3 matchers expected, 1 recorded
For curiosity, this works well in a debugger when I put a breakpoint before docRepository.findByDocType
.
I found this Mockito ticket, which looks relevant but I did not understand it and I don’t know how to apply it to my situation.