Tinylog provider with a mock instance backed by Mockito.
Warning
This library does not support parallel test execution.
Unlike the traditional approach where each class or even instance has its own named logger, tinylog uses a singleton logger. And the provider implementation is a bit complicated to check access to the logger. Therefore, I recommend using the Writer for testing.
Just put a test dependency to your POM:
<dependency>
<artifactId>mock-loggers-tinylog-provider</artifactId>
<groupId>io.github.vitalijr2.logging</groupId>
<scope>test</scope>
<version>1.2.0</version>
</dependency>
Use the MockTinylogProvider
annotation to access the mock provider.
The simplest usage example looks like this:
@MockTinylogProvider
private static LoggingProvider logger;
@Test
void helloWorld() {
when(logger.getMinimumLevel(isNull())).thenReturn(Level.INFO);
var helloService = new HelloService();
assertDoesNotThrow(helloService::sayHelloWorld);
verify(logger).log(anyInt(), isNull(), eq(Level.INFO), isNull(), isNull(), anyString(), isNull());
}
See more details at HelloServiceBasicTest.java
Important
Keep in mind that the logger is initialized only once during the test run.
Therefore, a more complex example cleans the logger after (or before) each test:
// the static logger instance
@MockTinylogProvider
private static LoggingProvider logger;
// clean the mock logger after each test
@AfterEach
void tearDown() {
clearInvocations(logger);
}
// use the mock logger in a test
@DisplayName("Names")
@ParameterizedTest(name = "<{0}>")
@ValueSource(strings = {"John", "Jane"})
void names(String name) {
when(logger.getMinimumLevel(isNull())).thenReturn(Level.INFO);
var helloService = new HelloService();
assertDoesNotThrow(() -> helloService.sayHello(name));
verify(logger).log(anyInt(), isNull(), eq(Level.INFO), isNull(), isNull(),
eq("Hello " + name + "!"), isNull());
}
See more details at HelloServiceFullTest.java
To avoid manual cleaning of the mock logger you can use the jUnit extension for automation:
@ExtendWith(MockLoggerExtension.class)
class HelloServiceExtensionTest {
@MockTinylogProvider
private static LoggingProvider logger;
@DisplayName("Names")
@ParameterizedTest(name = "<{0}>")
@ValueSource(strings = {"John", "Jane"})
void names(String name) {
when(logger.getMinimumLevel(isNull())).thenReturn(Level.INFO);
var helloService = new HelloService();
assertDoesNotThrow(() -> helloService.sayHello(name));
verify(logger).log(anyInt(), isNull(), eq(Level.INFO), isNull(), isNull(),
eq("Hello " + name + "!"), isNull());
}
}
See more details at HelloServiceExtensionTest.java
Also you can use the annotation for automation:
@MockLoggers
class HelloServiceAnnotationTest {
@MockTinylogProvider
private static LoggingProvider logger;
@DisplayName("Names")
@ParameterizedTest(name = "<{0}>")
@ValueSource(strings = {"John", "Jane"})
void names(String name) {
when(logger.getMinimumLevel(isNull())).thenReturn(Level.INFO);
var helloService = new HelloService();
assertDoesNotThrow(() -> helloService.sayHello(name));
verify(logger).log(anyInt(), isNull(), eq(Level.INFO), isNull(), isNull(),
eq("Hello " + name + "!"), isNull());
}
}
See more details at HelloServiceAnnotationTest.java
This library can also inject a mock provider instance as a parameter of a test method:
@ExtendWith({MockLoggerExtension.class,MockTinylogProviderExtension.class})
class HelloServiceParameterTest {
@DisplayName("Hello world")
@Test
void helloWorld(LoggingProvider logger) {
when(logger.getMinimumLevel(isNull())).thenReturn(Level.INFO);
var helloService = new HelloService();
assertDoesNotThrow(helloService::sayHelloWorld);
verify(logger).log(anyInt(), isNull(), eq(Level.INFO), isNull(), isNull(), anyString(), isNull());
}
}
See more details ad HelloServiceParameterTest.java
If your application is bundled with another tinylog provider, and it is present on the test classpath, use the configuration to specify the use of the mock provider. See tinylog.properties.