-
Notifications
You must be signed in to change notification settings - Fork 187
Stop swallowing System.err in JUnit tests #2685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,9 +19,11 @@ | |
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.junit.jupiter.api.Assertions.fail; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.PrintStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.HashMap; | ||
|
|
@@ -764,4 +766,26 @@ static <T> T runOperationInThread(int timeoutMs, ExceptionalSupplier<T> supplier | |
| T result = (T) supplierValue[0]; | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Capture any output on System.err | ||
| * | ||
| * This method does not capture output on stderr from C level, such as | ||
| * Gdk-CRITICAL messages. | ||
| * | ||
| * @param runnable to run while capturing output | ||
| * @return output on System.err | ||
| */ | ||
| public static String runWithCapturedStderr(Runnable runnable) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW, I've recently relied on AutoCloseable a lot for testing; it allows some good way of wrapping state relying less on Runnable. For this case, it could go that way class CaptureStderr implements AutoCloseable, Supplier<String> {
ByteArrayOutputStream errContent
CaptureStderr() {
errContent = new ByteArrayOutputStream();
System.setErr(new PrintStream(errContent, true, StandardCharsets.UTF_8));
}
@Override
public void close() {
System.setErr(stderr);
errContent.close();
}
@Override
public String get() {
return errContent.toString(StandardCharsets.UTF_8);
}
}and then tests can be written try (new captureStdErr = new CaptureStdErr()) {
// do stuff
assertTrue(captureStdErr.get().contains("blah");
}
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have been meaning to come back to thank you for this advice - while I have not updated this now closed PR, I have used this methodology in my latest testing PR: #2825 |
||
| PrintStream originalErr = System.err; | ||
| ByteArrayOutputStream errContent = new ByteArrayOutputStream(); | ||
| System.setErr(new PrintStream(errContent, true, StandardCharsets.UTF_8)); | ||
| try { | ||
| runnable.run(); | ||
| return errContent.toString(StandardCharsets.UTF_8); | ||
|
|
||
| } finally { | ||
| System.setErr(originalErr); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI @SyntevoAlex I found out where System.err was going.