16
16
17
17
package org .springframework .boot .logging .logback ;
18
18
19
- import java .util .List ;
20
19
import java .util .function .Supplier ;
21
20
22
21
import ch .qos .logback .classic .LoggerContext ;
22
+ import ch .qos .logback .core .BasicStatusManager ;
23
23
import ch .qos .logback .core .status .ErrorStatus ;
24
24
import ch .qos .logback .core .status .InfoStatus ;
25
25
import ch .qos .logback .core .status .Status ;
28
28
import ch .qos .logback .core .status .WarnStatus ;
29
29
import org .junit .jupiter .api .Test ;
30
30
import org .junit .jupiter .api .extension .ExtendWith ;
31
- import org .mockito .ArgumentCaptor ;
32
31
33
32
import org .springframework .boot .testsupport .system .CapturedOutput ;
34
33
import org .springframework .boot .testsupport .system .OutputCaptureExtension ;
35
34
import org .springframework .test .util .ReflectionTestUtils ;
36
35
37
36
import static org .assertj .core .api .Assertions .assertThat ;
38
- import static org .mockito .ArgumentMatchers .any ;
39
- import static org .mockito .BDDMockito .given ;
40
- import static org .mockito .BDDMockito .then ;
41
- import static org .mockito .Mockito .mock ;
42
37
43
38
/**
44
39
* Tests for {@link SystemStatusListener}.
@@ -51,13 +46,12 @@ class SystemStatusListenerTests {
51
46
52
47
private static final String TEST_MESSAGE = "testtesttest" ;
53
48
54
- private final StatusManager statusManager = mock ( StatusManager . class );
49
+ private final StatusManager statusManager = new BasicStatusManager ( );
55
50
56
- private final LoggerContext loggerContext = mock ( LoggerContext . class );
51
+ private final LoggerContext loggerContext = new LoggerContext ( );
57
52
58
53
SystemStatusListenerTests () {
59
- given (this .loggerContext .getStatusManager ()).willReturn (this .statusManager );
60
- given (this .statusManager .add (any (StatusListener .class ))).willReturn (true );
54
+ this .loggerContext .setStatusManager (this .statusManager );
61
55
}
62
56
63
57
@ Test
@@ -104,8 +98,9 @@ void addStatusWithErrorLevelWhenDebugPrintsToSystemOut(CapturedOutput output) {
104
98
105
99
@ Test
106
100
void shouldRetrospectivePrintStatusOnStartAndDebugIsDisabled (CapturedOutput output ) {
107
- given (this .statusManager .getCopyOfStatusList ()).willReturn (List .of (new ErrorStatus (TEST_MESSAGE , null ),
108
- new WarnStatus (TEST_MESSAGE , null ), new InfoStatus (TEST_MESSAGE , null )));
101
+ this .statusManager .add (new ErrorStatus (TEST_MESSAGE , null ));
102
+ this .statusManager .add (new WarnStatus (TEST_MESSAGE , null ));
103
+ this .statusManager .add (new InfoStatus (TEST_MESSAGE , null ));
109
104
addStatus (false , () -> new InfoStatus (TEST_MESSAGE , null ));
110
105
assertThat (output .getErr ()).contains ("WARN " + TEST_MESSAGE );
111
106
assertThat (output .getErr ()).contains ("ERROR " + TEST_MESSAGE );
@@ -115,8 +110,9 @@ void shouldRetrospectivePrintStatusOnStartAndDebugIsDisabled(CapturedOutput outp
115
110
116
111
@ Test
117
112
void shouldRetrospectivePrintStatusOnStartAndDebugIsEnabled (CapturedOutput output ) {
118
- given (this .statusManager .getCopyOfStatusList ()).willReturn (List .of (new ErrorStatus (TEST_MESSAGE , null ),
119
- new WarnStatus (TEST_MESSAGE , null ), new InfoStatus (TEST_MESSAGE , null )));
113
+ this .statusManager .add (new ErrorStatus (TEST_MESSAGE , null ));
114
+ this .statusManager .add (new WarnStatus (TEST_MESSAGE , null ));
115
+ this .statusManager .add (new InfoStatus (TEST_MESSAGE , null ));
120
116
addStatus (true , () -> new InfoStatus (TEST_MESSAGE , null ));
121
117
assertThat (output .getErr ()).isEmpty ();
122
118
assertThat (output .getOut ()).contains ("WARN " + TEST_MESSAGE );
@@ -128,18 +124,17 @@ void shouldRetrospectivePrintStatusOnStartAndDebugIsEnabled(CapturedOutput outpu
128
124
void shouldNotRetrospectivePrintWhenStatusIsOutdated (CapturedOutput output ) {
129
125
ErrorStatus outdatedStatus = new ErrorStatus (TEST_MESSAGE , null );
130
126
ReflectionTestUtils .setField (outdatedStatus , "timestamp" , System .currentTimeMillis () - 300 );
131
- given ( this .statusManager .getCopyOfStatusList ()). willReturn ( List . of ( outdatedStatus ) );
127
+ this .statusManager .add ( outdatedStatus );
132
128
addStatus (false , () -> new InfoStatus (TEST_MESSAGE , null ));
133
129
assertThat (output .getOut ()).isEmpty ();
134
130
assertThat (output .getErr ()).isEmpty ();
135
131
}
136
132
137
133
private void addStatus (boolean debug , Supplier <Status > statusFactory ) {
138
134
SystemStatusListener .addTo (this .loggerContext , debug );
139
- ArgumentCaptor <StatusListener > listener = ArgumentCaptor .forClass (StatusListener .class );
140
- then (this .statusManager ).should ().add (listener .capture ());
141
- assertThat (listener .getValue ()).extracting ("context" ).isSameAs (this .loggerContext );
142
- listener .getValue ().addStatusEvent (statusFactory .get ());
135
+ StatusListener listener = this .statusManager .getCopyOfStatusListenerList ().get (0 );
136
+ assertThat (listener ).extracting ("context" ).isSameAs (this .loggerContext );
137
+ listener .addStatusEvent (statusFactory .get ());
143
138
}
144
139
145
140
}
0 commit comments