Skip to content

Provides some classes simplifying the configuration and asserting of logging in the context of unit-tests

License

Notifications You must be signed in to change notification settings

cuioss/cui-test-juli-logger

cui-test-juli-logger

What is it?

Provides some classes simplifying the configuration and asserting of logging in the context of unit-tests.

Maven Coordinates

    <dependency>
        <groupId>de.cuioss.test</groupId>
        <artifactId>cui-test-juli-logger</artifactId>
    </dependency>

How To use it

Enable for a unit-test

@EnableTestLogger
class PortalHealthServletTest {}

Configure for all tests

@EnableTestLogger(rootLevel = TestLogLevel.INFO, trace = List.class, error = Set.class)
class PortalHealthServletTest {}

The logger and level are resetted for each test

Asserting log statements: written or not:

@EnableTestLogger
class PortalHealthServletTest {

    @Test
    void shouldAssertLogs() {
        LogAsserts.assertLogMessagePresent(TestLogLevel.DEBUG, "Should be there at least once");
        LogAsserts.assertSingleLogMessagePresent(TestLogLevel.INFO, "Should be there exactly once");
        LogAsserts.assertLogMessagePresentContaining(TestLogLevel.INFO, "part of the expected message");
        LogAsserts.assertNoLogMessagePresent(TestLogLevel.WARN, PortalHealthServlet.class);
        // and many more asserts
    }

Changing LogLevel dynamically:

    TestLogLevel.DEBUG.addLogger(PortalHealthServlet.class);
    // Set Root-level to debug
    TestLogLevel.DEBUG.setAsRootLevel();

Advanced Usage:

    // Programmatic Setup and teardown, usually done by EnableTestLogger / TestLoggerController
   TestLoggerFactory.install();
   TestLoggerFactory.uninstall();

   // Access the TestLogHandler (for advanced queries not covered by LogAsserts)
   TestLoggerFactory.getTestHandler();

About

Provides some classes simplifying the configuration and asserting of logging in the context of unit-tests

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Languages