Skip to content
This repository has been archived by the owner on Nov 27, 2024. It is now read-only.

JDK Platform Logging Service with mock loggers backed by Mockito.

License

Notifications You must be signed in to change notification settings

vitalijr2/mock-jdk-platform-logging

Repository files navigation

Mock of JDK Platform Logging

JDK Platform Logging Service with mock loggers backed by Mockito.

Important

Please use mock-loggers-jdk-platform-logging instead.

Java Version jUnit Version Mockito Version License
Maven Central Last Update Maven Central Javadoc

How to use

Just put a test dependency to your POM:

<dependency>
    <artifactId>mock-jdk-platform-logging</artifactId>
    <groupId>io.github.vitalijr2.logging</groupId>
    <scope>test</scope>
    <version>1.1.3</version>
</dependency>

The most basic usage example looks like this:

@Test
void helloWorld() {
    var helloService = new HelloService();

    assertDoesNotThrow(helloService::sayHelloWorld);

    verify(System.getLogger("HelloService")).log(Level.INFO, "Hello World!");
}

See more details at HelloServiceBasicTest.java

It should be taken into account that all loggers are initialized only once during the run of tests. Therefore, a more complex example cleans the loggers before (or after) each test:

// the static logger instance
private static Logger logger;

// initialize the mock logger once
@BeforeAll
static void setUpClass() {
    logger = System.getLogger("HelloService");
}

// 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) {
    var helloService = new HelloService();

    assertDoesNotThrow(() -> helloService.sayHello(name));

    var logger = System.getLogger("HelloService");

    verify(logger).log(Level.INFO, "Hello " + name + "!");
    verifyNoMoreInteractions(logger);
}

See more details at HelloServiceFullTest.java

Since the version 1.1.0 you can use the jUnit extension for automation.

@ExtendWith(MockLoggerExtension.class)
class HelloServiceExtensionTest {

    private static Logger logger;

    @BeforeAll
    static void setUpClass() {
        logger = System.getLogger("HelloService");
    }

    @DisplayName("Names")
    @ParameterizedTest(name = "<{0}>")
    @ValueSource(strings = {"John", "Jane"})
    void names(String name) {
        var helloService = new HelloService();

        assertDoesNotThrow(() -> helloService.sayHello(name));

        var logger = System.getLogger("HelloService");

        verify(logger).log(Level.INFO, "Hello " + name + "!");
        verifyNoMoreInteractions(logger);
    }

}

See more details at HelloServiceExtensionTest.java

Since the version 1.1.3 you can use the annotation for automation.

@MockLoggers
class HelloServiceAnnotationTest {

    private static Logger logger;

    @BeforeAll
    static void setUpClass() {
        logger = System.getLogger("HelloService");
    }

    @DisplayName("Names")
    @ParameterizedTest(name = "<{0}>")
    @ValueSource(strings = {"John", "Jane"})
    void names(String name) {
        var helloService = new HelloService();

        assertDoesNotThrow(() -> helloService.sayHello(name));

        var logger = System.getLogger("HelloService");

        verify(logger).log(Level.INFO, "Hello " + name + "!");
        verifyNoMoreInteractions(logger);
    }

}

See more details at HelloServiceAnnotationTest.java

Credits

There are two projects which inspired me to make this library:

Contributing

Please read Contributing.

History

See Changelog

License

Copyright 2024 Vitalij Berdinskih

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Apache License v2.0