Skip to content

Commit 1d4aaf2

Browse files
Suppress expected RuntimeException in AbstractMediaTest using TestLogger (#4256)
This change addresses an issue where `AbstractMediaTest.testConcurrentPauseAsyncError` prints a `java.lang.RuntimeException: Fail` stack trace to stderr during execution, creating noise in CI logs. This exception is expected as part of the test logic but was not being intercepted. We now use `TestLogger.install()` to capture the exception, verify it matches the expected "Fail" message, and then clear it, preventing it from being printed to the console. This ensures cleaner test output while maintaining the integrity of the error handling test case. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 3bbbf0c commit 1d4aaf2

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

maven/core-unittests/src/test/java/com/codename1/media/AbstractMediaTest.java

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.codename1.media;
22

3+
import com.codename1.junit.TestLogger;
34
import com.codename1.junit.UITestBase;
45
import com.codename1.junit.FormTest;
56
import com.codename1.ui.Display;
@@ -104,19 +105,40 @@ public void testConcurrentPauseAsync() {
104105

105106
@FormTest
106107
public void testConcurrentPauseAsyncError() {
107-
TestMedia media = new TestMedia();
108-
media.playing = true;
109-
110-
AsyncMedia.PauseRequest req1 = media.pauseAsync();
111-
AsyncMedia.PauseRequest req2 = media.pauseAsync();
112-
113-
Exception ex = new RuntimeException("Fail");
114-
media.fireMediaError(new AsyncMedia.MediaException(AsyncMedia.MediaErrorType.Unknown, ex));
115-
116-
assertTrue(req1.isDone());
117-
assertTrue(req2.isDone());
118-
119-
assertThrows(AsyncResource.AsyncExecutionException.class, () -> req1.get());
120-
assertThrows(AsyncResource.AsyncExecutionException.class, () -> req2.get());
108+
TestLogger.install();
109+
try {
110+
TestMedia media = new TestMedia();
111+
media.playing = true;
112+
113+
AsyncMedia.PauseRequest req1 = media.pauseAsync();
114+
AsyncMedia.PauseRequest req2 = media.pauseAsync();
115+
116+
Exception ex = new RuntimeException("Fail");
117+
media.fireMediaError(new AsyncMedia.MediaException(AsyncMedia.MediaErrorType.Unknown, ex));
118+
119+
assertTrue(req1.isDone());
120+
assertTrue(req2.isDone());
121+
122+
assertThrows(AsyncResource.AsyncExecutionException.class, () -> req1.get());
123+
assertThrows(AsyncResource.AsyncExecutionException.class, () -> req2.get());
124+
125+
// Check if TestLogger caught the exception
126+
boolean exceptionCaught = false;
127+
for (Throwable t : TestLogger.getThrowables()) {
128+
if (t.getMessage() != null && t.getMessage().contains("Fail")) {
129+
exceptionCaught = true;
130+
break;
131+
}
132+
// Check cause too
133+
if (t.getCause() != null && t.getCause().getMessage() != null && t.getCause().getMessage().contains("Fail")) {
134+
exceptionCaught = true;
135+
break;
136+
}
137+
}
138+
assertTrue(exceptionCaught, "The expected exception should have been logged");
139+
TestLogger.getThrowables().clear();
140+
} finally {
141+
TestLogger.remove();
142+
}
121143
}
122144
}

0 commit comments

Comments
 (0)