Skip to content

Conversation

@jonahgraham
Copy link
Contributor

test_printStackTrace overrides System.err with its own print stream to check functionality. But these tests did not restore System.err, so all System.err output for the rest of the test suite was lost into the last of these ByteArrayOutputStreams that was created.

The tests themselves have little intrinsic value as they really test nothing of SWT itself, but it does help achieve code coverage.

This was discovered when working on #2684 as I could not see the output when running all the tests.

jonahgraham added a commit to jonahgraham/eclipse.platform.swt that referenced this pull request Oct 28, 2025
While working on eclipse-platform#2685
and specifically trying to verify that change fixes the comment below \*
I realized that a migration step was missed changing the suites from
JUnit4 to JUnit5.

On JUnit5 the `@BeforeAll` and `@AfterAll` only apply to the tests in
the class itself, not to `@SelectClasses` too.

Instead JUnit5 provides `@BeforeSuite` and `@AfterSuite`

\* this comment:

> For some reason, printing to System.err in JUnit test has no effect
test_printStackTrace overrides System.err with its own print stream to
check functionality. But these tests did not restore System.err, so
all System.err output for the rest of the test suite was lost into
the last of these ByteArrayOutputStreams that was created.

The tests themselves have little intrinsic value as they really test
nothing of SWT itself, but it does help achieve code coverage.

This was discovered when working on eclipse-platform#2684
as I could not see the output when running all the tests.

Since we no longer swallow errors, the "For some reason, printing
to System.err[...]" comment is resolved and we can print leaks to
stderr again.
@github-actions
Copy link
Contributor

github-actions bot commented Oct 28, 2025

Test Results

  111 files   -  7    111 suites   - 7   14m 19s ⏱️ +19s
4 591 tests  - 56  4 575 ✅  - 54  15 💤  - 3  1 ❌ +1 
  274 runs   - 56    273 ✅  - 53   1 💤  - 3  0 ❌ ±0 

For more details on these failures, see this check.

Results for commit 785c322. ± Comparison against base commit c308fd1.

This pull request removes 56 tests.
AllWin32Tests ImageWin32Tests ‑ testDisposeDrawnImageBeforeRequestingTargetForOtherZoom
AllWin32Tests ImageWin32Tests ‑ testDrawImageAtDifferentZooms(boolean)[1] true
AllWin32Tests ImageWin32Tests ‑ testDrawImageAtDifferentZooms(boolean)[2] false
AllWin32Tests ImageWin32Tests ‑ testImageDataForDifferentFractionalZoomsShouldBeDifferent
AllWin32Tests ImageWin32Tests ‑ testImageShouldHaveDimesionAsPerZoomLevel
AllWin32Tests ImageWin32Tests ‑ testRetrieveImageDataAtDifferentZooms(boolean)[1] true
AllWin32Tests ImageWin32Tests ‑ testRetrieveImageDataAtDifferentZooms(boolean)[2] false
AllWin32Tests ImageWin32Tests ‑ test_getImageData_fromCopiedImage
AllWin32Tests ImageWin32Tests ‑ test_getImageData_fromImageForImageDataFromImage
AllWin32Tests TestTreeColumn ‑ test_ColumnOrder
…

♻️ This comment has been updated with latest results.

@jonahgraham jonahgraham force-pushed the stop-swallowing-stderr branch from c8dfa71 to 785c322 Compare October 28, 2025 18:44
jonahgraham added a commit to jonahgraham/eclipse.platform.swt that referenced this pull request Oct 28, 2025
While working on eclipse-platform#2685
and specifically trying to verify that change fixes the comment below \*
I realized that a migration step was missed changing the suites from
JUnit4 to JUnit5.

On JUnit5 the `@BeforeAll` and `@AfterAll` only apply to the tests in
the class itself, not to `@SelectClasses` too.

Instead JUnit5 provides `@BeforeSuite` and `@AfterSuite`

\* this comment:

> For some reason, printing to System.err in JUnit test has no effect
for (Error leak : leakedResources) {
// For some reason, printing to System.err in JUnit test has no effect
leak.printStackTrace (System.out);
leak.printStackTrace ();
Copy link
Contributor Author

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.

@jonahgraham jonahgraham merged commit 92db85c into eclipse-platform:master Oct 28, 2025
21 of 22 checks passed
@jonahgraham jonahgraham deleted the stop-swallowing-stderr branch October 28, 2025 19:40
jonahgraham added a commit that referenced this pull request Oct 29, 2025
While working on #2685
and specifically trying to verify that change fixes the comment below \*
I realized that a migration step was missed changing the suites from
JUnit4 to JUnit5.

On JUnit5 the `@BeforeAll` and `@AfterAll` only apply to the tests in
the class itself, not to `@SelectClasses` too.

Instead JUnit5 provides `@BeforeSuite` and `@AfterSuite`

\* this comment:

> For some reason, printing to System.err in JUnit test has no effect
* @param runnable to run while capturing output
* @return output on System.err
*/
public static String runWithCapturedStderr(Runnable runnable) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants