An open API service indexing awesome lists of open source software.

https://github.com/cuioss/cui-test-juli-logger

Provides some classes simplifying the configuration and asserting of logging in the context of unit-tests
https://github.com/cuioss/cui-test-juli-logger

java junit-jupiter logging

Last synced: 8 months ago
JSON representation

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

Awesome Lists containing this project

README

          

= cui-test-juli-logger

== Status

image:https://github.com/cuioss/cui-test-juli-logger/actions/workflows/maven.yml/badge.svg[Java CI with Maven,link=https://github.com/cuioss/cui-test-juli-logger/actions/workflows/maven.yml]
image:http://img.shields.io/:license-apache-blue.svg[License,link=http://www.apache.org/licenses/LICENSE-2.0.html]
image:https://img.shields.io/maven-central/v/de.cuioss.test/cui-test-juli-logger.svg[Maven Central,link=https://central.sonatype.com/artifact/de.cuioss.test/cui-test-juli-logger]

https://sonarcloud.io/summary/new_code?id=cuioss_cui-test-juli-logger[image:https://sonarcloud.io/api/project_badges/measure?project=cuioss_cui-test-juli-logger&metric=alert_status[Quality
Gate Status]]
image:https://sonarcloud.io/api/project_badges/measure?project=cuioss_cui-test-juli-logger&metric=ncloc[Lines of Code,link=https://sonarcloud.io/summary/new_code?id=cuioss_cui-test-juli-logger]
image:https://sonarcloud.io/api/project_badges/measure?project=cuioss_cui-test-juli-logger&metric=coverage[Coverage,link=https://sonarcloud.io/summary/new_code?id=cuioss_cui-test-juli-logger]

https://cuioss.github.io/cui-test-juli-logger/about.html[Generated Documentation on github-pages]

== What is it?

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

=== Maven Coordinates

[source,xml]
----

de.cuioss.test
cui-test-juli-logger

----

== How To use it

Enable for a unit-test

[source,java]
----
@EnableTestLogger
class PortalHealthServletTest {}
----

Configure for all tests

[source,java]
----
@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:

[source,java]
----
@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:

[source,java]
----
TestLogLevel.DEBUG.addLogger(PortalHealthServlet.class);
// Set Root-level to debug
TestLogLevel.DEBUG.setAsRootLevel();
----

Advanced Usage:

[source,java]
----
// 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();
----