I have a class that has the @Slf4j annotation.
I try to write a test and mock the Logger, but it does not work.
@RequiredArgsConstructor
@Slf4j
public abstract class ExampleClass {
protected final PropsClass properties;
protected void logInfo(..) {
log.info(...);
clearMappedDiagnosticContext();
}
}
This is how the test looks like:
@RunWith(MockitoJUnitRunner.class)
public class ExampleClassTest {
@Mock
Logger logger;
@Mock
PropsClass properties;
@InjectMocks
ExampleClass exampleClass;
@Test
public void logSomethingtest() {
...
exampleClass.logInfo(...);
Mockito.verify(logger).info(marker, "foo bar {}", ...);
}
This is the error I get:
Wanted but not invoked:
logger.info(
MY_MARKER,
"..........",
"....",
"....",
0L
);
Actually, there were zero interactions with this mock.
The question is, how to mock the Logger?
slf4j-test
. – Commonable