Skip to content

Commit be74d92

Browse files
committed
Updated LoggingFilterTest to check the java.util.logging.Logger instead of System.out, reflecting recent changes to LoggingFilter (#95)
1 parent 0863976 commit be74d92

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

src/test/java/org/juv25d/filter/LoggingFilterTest.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ void callsNextFilterInChain() throws IOException {
2626

2727
@Test
2828
void logsHttpMethodAndPath() throws IOException {
29-
3029
LoggingFilter filter = new LoggingFilter();
3130
HttpRequest req = mock(HttpRequest.class);
3231
HttpResponse res = mock(HttpResponse.class);
@@ -35,17 +34,28 @@ void logsHttpMethodAndPath() throws IOException {
3534
when(req.method()).thenReturn("GET");
3635
when(req.path()).thenReturn("/test");
3736

38-
var originalOut = System.out;
39-
var out = new java.io.ByteArrayOutputStream();
40-
System.setOut(new java.io.PrintStream(out));
37+
java.util.logging.Logger logger = org.juv25d.logging.ServerLogging.getLogger();
38+
java.util.List<java.util.logging.LogRecord> records = new java.util.ArrayList<>();
39+
java.util.logging.Handler handler = new java.util.logging.Handler() {
40+
@Override
41+
public void publish(java.util.logging.LogRecord record) {
42+
records.add(record);
43+
}
44+
@Override
45+
public void flush() {}
46+
@Override
47+
public void close() throws SecurityException {}
48+
};
49+
logger.addHandler(handler);
4150

4251
try {
4352
filter.doFilter(req, res, chain);
4453

45-
String output = out.toString();
46-
assertTrue(output.contains("GET /test"), "Output should contain logged method and path");
54+
boolean found = records.stream()
55+
.anyMatch(r -> r.getMessage().contains("GET /test"));
56+
assertTrue(found, "Logger should have captured the method and path");
4757
} finally {
48-
System.setOut(originalOut);
58+
logger.removeHandler(handler);
4959
}
5060
}
5161
}

0 commit comments

Comments
 (0)