-
Notifications
You must be signed in to change notification settings - Fork 1
Description
Description
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");
}
}
@ExtendWith(SystemErrGuard.class)
class TestClass {
@Test
void firstTestCase(final Capturable stream) {
stream.capture();
TesteableClass.testeableMethod();
assertThat(stream.getCapturedData(), containsString("TESTEABLE LOG MESSAGE"));
}
@Test
void secondTestCase(final Capturable stream) {
stream.capture();
TesteableClass.testeableMethod();
assertThat(stream.getCapturedData(), containsString("TESTEABLE LOG MESSAGE"));
}
}
- Execute the tests.
- 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
ConsoleHandlerbefore 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:
@BeforeEach
void beforeEach() {
TesteableClass.LOGGER.setUseParentHandlers(false);
final Handler[] handlers = TesteableClass.LOGGER.getHandlers();
for (final Handler handler : handlers) {
TesteableClass.LOGGER.removeHandler(handler);
}
TesteableClass.LOGGER.addHandler(new ConsoleHandler());
}
Expected behavior
Tests should pass and "TESTEABLE LOG MESSAGE" should be visible twice in the console.
Environment
- JUnit5 System Extensions: 1.1.0
- OS: Ubuntu 20.04.1
- Java Version: 11
- Maven Version (in case of build problems only): 3.6.3