11package test .org .fugerit .java .core .db .dao ;
22
33import java .sql .SQLException ;
4+ import java .util .ArrayList ;
5+ import java .util .List ;
46
7+ import lombok .extern .slf4j .Slf4j ;
8+ import org .apache .logging .log4j .LogManager ;
9+ import org .apache .logging .log4j .core .LogEvent ;
10+ import org .apache .logging .log4j .core .Logger ;
11+ import org .apache .logging .log4j .core .appender .AbstractAppender ;
12+ import org .apache .logging .log4j .core .config .Property ;
513import org .fugerit .java .core .db .dao .DAORuntimeException ;
614import org .junit .jupiter .api .Assertions ;
715import org .junit .jupiter .api .Test ;
816
17+ import static org .junit .jupiter .api .Assertions .*;
18+ import static org .junit .jupiter .api .Assertions .assertTrue ;
19+
20+ @ Slf4j
921class TestDAORuntimeException {
1022
1123 @ Test
@@ -15,7 +27,52 @@ void testApply() {
1527
1628 @ Test
1729 void testApplySilent () {
18- DAORuntimeException .applySilent ( () -> { throw new SQLException ( "junit test scenario apply silent" ); });
30+ // Create a custom appender to capture log events
31+ List <LogEvent > logEvents = new ArrayList <>();
32+
33+ AbstractAppender testAppender = new AbstractAppender ("TestAppender" , null , null , true , Property .EMPTY_ARRAY ) {
34+ @ Override
35+ public void append (LogEvent event ) {
36+ logEvents .add (event .toImmutable ());
37+ }
38+ };
39+ testAppender .start ();
40+
41+ // Get the logger and add our appender
42+ Logger logger = (Logger ) LogManager .getLogger (DAORuntimeException .class );
43+ logger .addAppender (testAppender );
44+ logger .setAdditive (true );
45+
46+ String errorMessage = "junit test scenario apply silent" ;
47+
48+ try {
49+ // Execute the method under test
50+ DAORuntimeException .applySilent (() -> {
51+ throw new SQLException (errorMessage );
52+ });
53+
54+ // Verify log event was captured
55+ assertEquals (1 , logEvents .size ());
56+
57+ LogEvent logEvent = logEvents .get (0 );
58+
59+ // Verify it's a WARN level
60+ assertEquals (org .apache .logging .log4j .Level .WARN , logEvent .getLevel ());
61+
62+ // Verify the message
63+ String message = logEvent .getMessage ().getFormattedMessage ();
64+ assertTrue (message .contains ("Exception on DAORuntimeException.applySilent()" ));
65+ assertTrue (message .contains (errorMessage ));
66+
67+ // Verify the throwable
68+ assertNotNull (logEvent .getThrown ());
69+ assertTrue (logEvent .getThrown () instanceof SQLException );
70+
71+ } finally {
72+ // Clean up
73+ logger .removeAppender (testAppender );
74+ testAppender .stop ();
75+ }
1976 }
2077
2178 @ Test
0 commit comments