3535import org .springframework .boot .logging .structured .StructuredLoggingJsonProperties .StackTrace ;
3636import org .springframework .boot .logging .structured .StructuredLoggingJsonProperties .StackTrace .Root ;
3737import org .springframework .boot .logging .structured .StructuredLoggingJsonProperties .StructuredLoggingJsonPropertiesRuntimeHints ;
38+ import org .springframework .core .env .Environment ;
3839import org .springframework .mock .env .MockEnvironment ;
3940import org .springframework .util .ClassUtils ;
4041
@@ -115,44 +116,44 @@ class StackTraceTests {
115116 @ Test
116117 void createPrinterWhenEmptyReturnsNull () {
117118 StackTrace properties = new StackTrace (null , null , null , null , null , null );
118- assertThat (properties .createPrinter ()).isNull ();
119+ assertThat (properties .createPrinter (new MockEnvironment () )).isNull ();
119120 }
120121
121122 @ Test
122123 void createPrinterWhenNoPrinterAndNotEmptyReturnsStandard () {
123124 StackTrace properties = new StackTrace (null , Root .LAST , null , null , null , null );
124- assertThat (properties .createPrinter ()).isInstanceOf (StandardStackTracePrinter .class );
125+ assertThat (properties .createPrinter (new MockEnvironment () )).isInstanceOf (StandardStackTracePrinter .class );
125126 }
126127
127128 @ Test
128129 void createPrinterWhenLoggingSystemReturnsNull () {
129130 StackTrace properties = new StackTrace ("logging-system" , null , null , null , null , null );
130- assertThat (properties .createPrinter ()).isNull ();
131+ assertThat (properties .createPrinter (new MockEnvironment () )).isNull ();
131132 }
132133
133134 @ Test
134135 void createPrinterWhenLoggingSystemRelaxedReturnsNull () {
135136 StackTrace properties = new StackTrace ("LoggingSystem" , null , null , null , null , null );
136- assertThat (properties .createPrinter ()).isNull ();
137+ assertThat (properties .createPrinter (new MockEnvironment () )).isNull ();
137138 }
138139
139140 @ Test
140141 void createPrinterWhenStandardReturnsStandardPrinter () {
141142 StackTrace properties = new StackTrace ("standard" , null , null , null , null , null );
142- assertThat (properties .createPrinter ()).isInstanceOf (StandardStackTracePrinter .class );
143+ assertThat (properties .createPrinter (new MockEnvironment () )).isInstanceOf (StandardStackTracePrinter .class );
143144 }
144145
145146 @ Test
146147 void createPrinterWhenStandardRelaxedReturnsStandardPrinter () {
147148 StackTrace properties = new StackTrace ("STANDARD" , null , null , null , null , null );
148- assertThat (properties .createPrinter ()).isInstanceOf (StandardStackTracePrinter .class );
149+ assertThat (properties .createPrinter (new MockEnvironment () )).isInstanceOf (StandardStackTracePrinter .class );
149150 }
150151
151152 @ Test
152153 void createPrinterWhenStandardAppliesCustomizations () {
153154 Exception exception = TestException .create ();
154155 StackTrace properties = new StackTrace (null , Root .FIRST , 300 , 2 , true , false );
155- StackTracePrinter printer = ((StandardStackTracePrinter ) properties .createPrinter ())
156+ StackTracePrinter printer = ((StandardStackTracePrinter ) properties .createPrinter (new MockEnvironment () ))
156157 .withLineSeparator ("\n " );
157158 String actual = TestException .withoutLineNumbers (printer .printStackTraceToString (exception ));
158159 assertThat (actual ).isEqualToNormalizingNewlines ("""
@@ -167,7 +168,7 @@ void createPrinterWhenStandardAppliesCustomizations() {
167168 void createPrinterWhenStandardWithHashesPrintsHash () {
168169 Exception exception = TestException .create ();
169170 StackTrace properties = new StackTrace (null , null , null , null , null , true );
170- StackTracePrinter printer = properties .createPrinter ();
171+ StackTracePrinter printer = properties .createPrinter (new MockEnvironment () );
171172 String actual = printer .printStackTraceToString (exception );
172173 assertThat (actual ).containsPattern ("<#[0-9a-z]{8}>" );
173174 }
@@ -176,16 +177,18 @@ void createPrinterWhenStandardWithHashesPrintsHash() {
176177 void createPrinterWhenClassNameCreatesPrinter () {
177178 Exception exception = TestException .create ();
178179 StackTrace properties = new StackTrace (TestStackTracePrinter .class .getName (), null , null , null , true , null );
179- StackTracePrinter printer = properties .createPrinter ();
180+ StackTracePrinter printer = properties .createPrinter (new MockEnvironment () );
180181 assertThat (printer .printStackTraceToString (exception )).isEqualTo ("java.lang.RuntimeException: exception" );
181182 }
182183
183184 @ Test
184185 void createPrinterWhenClassNameInjectsConfiguredPrinter () {
185186 Exception exception = TestException .create ();
187+ MockEnvironment environment = new MockEnvironment ();
188+ environment .setProperty ("stracktracelineseparator" , "!" );
186189 StackTrace properties = new StackTrace (TestStackTracePrinterCustomized .class .getName (), Root .FIRST , 300 , 2 ,
187190 true , null );
188- StackTracePrinter printer = properties .createPrinter ();
191+ StackTracePrinter printer = properties .createPrinter (environment );
189192 String actual = TestException .withoutLineNumbers (printer .printStackTraceToString (exception ));
190193 assertThat (actual ).isEqualTo ("RuntimeExceptionroot! at org.springfr..." );
191194 }
@@ -237,9 +240,9 @@ static class TestStackTracePrinterCustomized implements StackTracePrinter {
237240
238241 private final StandardStackTracePrinter printer ;
239242
240- TestStackTracePrinterCustomized (StandardStackTracePrinter printer ) {
243+ TestStackTracePrinterCustomized (StandardStackTracePrinter printer , Environment environment ) {
241244 this .printer = printer .withMaximumLength (40 )
242- .withLineSeparator ("!" )
245+ .withLineSeparator (environment . getProperty ( "stracktracelineseparator" , " \n " ) )
243246 .withFormatter ((throwable ) -> ClassUtils .getShortName (throwable .getClass ()) + throwable .getMessage ());
244247 }
245248
0 commit comments