You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In test classes annotated with @ExtendWith(SystemErrGuard.class) and with multiple test cases that sniff the System.Err, the logs are truncated after the execution of the first test case.
An instance of java.util.logging.Logger uses by default ConsoleHandler that is bounded to a reference of the System.Err.
This binding happens upon creation of the Consolehandler, which is lazily instantiated, for example when logging a message with the logger for the first time (ie, LOGGER.info("message")).
If the System.Err is changed (ie, System.setErr(newSystemStream)), all the already existent instances of ConsoleHandler are not updated, so they don't log to the new System.Err.
Steps to Reproduce
Implement the following classes
import java.util.logging.Logger;
public final class TesteableClass {
public static final Logger LOGGER = Logger.getLogger(TesteableClass.class.getName());
public static String testeableMethod() {
LOGGER.info("TESTEABLE LOG MESSAGE");
}
}
Verify the console does not show all the logs (you can see in the console only one "TESTEABLE LOG MESSAGE" message instead of two)
As a work around, you can reset the ConsoleHandler before executing each tests and disabling the use of the parent ConsoleHandler (cause if not it would log twice, once for each handler, during the execution of the first test), for example:
We discussed moving the injection of the log sniffer from beforeEach to beforeAll. This fixes the issue described above at first glance.
But, in case multiple test classes test code that uses the same logger the issue could still appear. (Singleton lifecycle is bound to JVM to test class)
So the only possible solution I can think of is creating a singleton, that injects the log sniffer directly at its creation (for all test classes).
This is, however, quite an invasive approach and could lead to unexpected behavior if something goes wrong. (Tests fail even so this plugin was used in a different test class)
Description
In test classes annotated with
@ExtendWith(SystemErrGuard.class)
and with multiple test cases that sniff theSystem.Err
, the logs are truncated after the execution of the first test case.An instance of
java.util.logging.Logger
uses by defaultConsoleHandler
that is bounded to a reference of theSystem.Err
.This binding happens upon creation of the
Consolehandler
, which is lazily instantiated, for example when logging a message with the logger for the first time (ie,LOGGER.info("message")
).If the
System.Err
is changed (ie,System.setErr(newSystemStream)
), all the already existent instances ofConsoleHandler
are not updated, so they don't log to the newSystem.Err
.Steps to Reproduce
ConsoleHandler
before executing each tests and disabling the use of the parentConsoleHandler
(cause if not it would log twice, once for each handler, during the execution of the first test), for example:Expected behavior
Tests should pass and "TESTEABLE LOG MESSAGE" should be visible twice in the console.
Environment
The text was updated successfully, but these errors were encountered: