Yet another way to testing logging in application which use SLF4J.
- this binding support for easy create
Mockito
mock forLogger
- call to
Logger
can be delegated to instance ofSimpleLogger
, so we can create standardsimplelogger.properties
- support for testing and mocking
MDC
- light transitive dependencies - only
slf4j-api
andmockito-core
- support testing in parallel in multi thread
- all the Magic are done by
Mockito
plugins, so you don't need to directly use class from this library - ease use
Add dependency to your project:
For SLF4J 1.7.x
<dependencies>
<dependency>
<groupId>org.simplify4u</groupId>
<artifactId>slf4j-mock</artifactId>
<version><!-- check relases page --></version>
<scope>test</scope>
</dependency>
<dependencies>
For SLF4J 2.x
<dependencies>
<dependency>
<groupId>org.simplify4u</groupId>
<artifactId>slf4j2-mock</artifactId>
<version><!-- check relases page --></version>
<scope>test</scope>
</dependency>
<dependencies>
Please remember, that you can only have one SLF4J
binding or provider on classpath,
in the most case you must replace org.slf4j:slf4j-simple
by org.simplify4u:slf4j-mock
.
Write test:
class MyTest {
@Mock
Logger logger;
@InjectMocks
Example sut;
@Test
public void logInfoShouldBeLogged() {
// when
sut.methodWithLogInfo(INFO_TEST_MESSAGE);
// then
verify(logger).info(INFO_TEST_MESSAGE);
verifyNoMoreInteractions(logger);
}
}
More information and examples you can find on site: