16
16
17
17
package org .springframework .boot .test .context .assertj ;
18
18
19
+ import java .io .BufferedReader ;
20
+ import java .io .IOException ;
21
+ import java .io .PrintWriter ;
22
+ import java .io .StringReader ;
23
+ import java .io .StringWriter ;
24
+
19
25
import org .assertj .core .api .AbstractAssert ;
20
26
import org .assertj .core .api .AbstractObjectArrayAssert ;
21
27
import org .assertj .core .api .AbstractObjectAssert ;
37
43
*
38
44
* @param <C> The application context type
39
45
* @author Phillip Webb
46
+ * @author Andy Wilkinson
40
47
* @since 2.0.0
41
48
* @see ApplicationContextRunner
42
49
* @see AssertableApplicationContext
@@ -70,9 +77,8 @@ public class ApplicationContextAssert<C extends ApplicationContext>
70
77
*/
71
78
public ApplicationContextAssert <C > hasBean (String name ) {
72
79
if (this .startupFailure != null ) {
73
- throwAssertionError (new BasicErrorMessageFactory (
74
- "%nExpecting:%n <%s>%nto have bean named:%n <%s>%nbut context failed to start" ,
75
- getApplicationContext (), name ));
80
+ throwAssertionError (contextFailedToStartWhenExpecting (
81
+ "to have bean named:%n <%s>" , name ));
76
82
}
77
83
if (findBean (name ) == null ) {
78
84
throwAssertionError (new BasicErrorMessageFactory (
@@ -96,9 +102,8 @@ public ApplicationContextAssert<C> hasBean(String name) {
96
102
*/
97
103
public ApplicationContextAssert <C > hasSingleBean (Class <?> type ) {
98
104
if (this .startupFailure != null ) {
99
- throwAssertionError (new BasicErrorMessageFactory (
100
- "%nExpecting:%n <%s>%nto have a single bean of type:%n <%s>%nbut context failed to start" ,
101
- getApplicationContext (), type ));
105
+ throwAssertionError (contextFailedToStartWhenExpecting (
106
+ "to have a single bean of type:%n <%s>" , type ));
102
107
}
103
108
String [] names = getApplicationContext ().getBeanNamesForType (type );
104
109
if (names .length == 0 ) {
@@ -127,9 +132,8 @@ public ApplicationContextAssert<C> hasSingleBean(Class<?> type) {
127
132
*/
128
133
public ApplicationContextAssert <C > doesNotHaveBean (Class <?> type ) {
129
134
if (this .startupFailure != null ) {
130
- throwAssertionError (new BasicErrorMessageFactory (
131
- "%nExpecting:%n <%s>%nnot to have any beans of type:%n <%s>%nbut context failed to start" ,
132
- getApplicationContext (), type ));
135
+ throwAssertionError (contextFailedToStartWhenExpecting (
136
+ "not to have any beans of type:%n <%s>" , type ));
133
137
}
134
138
String [] names = getApplicationContext ().getBeanNamesForType (type );
135
139
if (names .length > 0 ) {
@@ -153,9 +157,8 @@ public ApplicationContextAssert<C> doesNotHaveBean(Class<?> type) {
153
157
*/
154
158
public ApplicationContextAssert <C > doesNotHaveBean (String name ) {
155
159
if (this .startupFailure != null ) {
156
- throwAssertionError (new BasicErrorMessageFactory (
157
- "%nExpecting:%n <%s>%nnot to have any beans of name:%n <%s>%nbut context failed to start" ,
158
- getApplicationContext (), name ));
160
+ throwAssertionError (contextFailedToStartWhenExpecting (
161
+ "not to have any beans of name:%n <%s>" , name ));
159
162
}
160
163
try {
161
164
Object bean = getApplicationContext ().getBean (name );
@@ -181,9 +184,8 @@ public ApplicationContextAssert<C> doesNotHaveBean(String name) {
181
184
*/
182
185
public <T > AbstractObjectArrayAssert <?, String > getBeanNames (Class <T > type ) {
183
186
if (this .startupFailure != null ) {
184
- throwAssertionError (new BasicErrorMessageFactory (
185
- "%nExpecting:%n <%s>%nto get beans names with type:%n <%s>%nbut context failed to start" ,
186
- getApplicationContext (), type ));
187
+ throwAssertionError (contextFailedToStartWhenExpecting (
188
+ "to get beans names with type:%n <%s>" , type ));
187
189
}
188
190
return Assertions .assertThat (getApplicationContext ().getBeanNamesForType (type ))
189
191
.as ("Bean names of type <%s> from <%s>" , type , getApplicationContext ());
@@ -207,9 +209,8 @@ public <T> AbstractObjectArrayAssert<?, String> getBeanNames(Class<T> type) {
207
209
*/
208
210
public <T > AbstractObjectAssert <?, T > getBean (Class <T > type ) {
209
211
if (this .startupFailure != null ) {
210
- throwAssertionError (new BasicErrorMessageFactory (
211
- "%nExpecting:%n <%s>%nto contain bean of type:%n <%s>%nbut context failed to start" ,
212
- getApplicationContext (), type ));
212
+ throwAssertionError (contextFailedToStartWhenExpecting (
213
+ "to contain bean of type:%n <%s>" , type ));
213
214
}
214
215
String [] names = getApplicationContext ().getBeanNamesForType (type );
215
216
if (names .length > 1 ) {
@@ -238,9 +239,8 @@ public <T> AbstractObjectAssert<?, T> getBean(Class<T> type) {
238
239
*/
239
240
public AbstractObjectAssert <?, Object > getBean (String name ) {
240
241
if (this .startupFailure != null ) {
241
- throwAssertionError (new BasicErrorMessageFactory (
242
- "%nExpecting:%n <%s>%nto contain a bean of name:%n <%s>%nbut context failed to start" ,
243
- getApplicationContext (), name ));
242
+ throwAssertionError (contextFailedToStartWhenExpecting (
243
+ "to contain a bean of name:%n <%s>" , name ));
244
244
}
245
245
Object bean = findBean (name );
246
246
return Assertions .assertThat (bean ).as ("Bean of name <%s> from <%s>" , name ,
@@ -267,9 +267,8 @@ public AbstractObjectAssert<?, Object> getBean(String name) {
267
267
@ SuppressWarnings ("unchecked" )
268
268
public <T > AbstractObjectAssert <?, T > getBean (String name , Class <T > type ) {
269
269
if (this .startupFailure != null ) {
270
- throwAssertionError (new BasicErrorMessageFactory (
271
- "%nExpecting:%n <%s>%nto contain a bean of name:%n <%s> (%s)%nbut context failed to start" ,
272
- getApplicationContext (), name , type ));
270
+ throwAssertionError (contextFailedToStartWhenExpecting (
271
+ "to contain a bean of name:%n <%s> (%s)" , name , type ));
273
272
}
274
273
Object bean = findBean (name );
275
274
if (bean != null && type != null && !type .isInstance (bean )) {
@@ -307,9 +306,8 @@ private Object findBean(String name) {
307
306
*/
308
307
public <T > MapAssert <String , T > getBeans (Class <T > type ) {
309
308
if (this .startupFailure != null ) {
310
- throwAssertionError (new BasicErrorMessageFactory (
311
- "%nExpecting:%n <%s>%nto get beans of type:%n <%s> (%s)%nbut context failed to start" ,
312
- getApplicationContext (), type , type ));
309
+ throwAssertionError (contextFailedToStartWhenExpecting (
310
+ "to get beans of type:%n <%s>" , type ));
313
311
}
314
312
return Assertions .assertThat (getApplicationContext ().getBeansOfType (type ))
315
313
.as ("Beans of type <%s> from <%s>" , type , getApplicationContext ());
@@ -357,9 +355,7 @@ public ApplicationContextAssert<C> hasFailed() {
357
355
*/
358
356
public ApplicationContextAssert <C > hasNotFailed () {
359
357
if (this .startupFailure != null ) {
360
- throwAssertionError (new BasicErrorMessageFactory (
361
- "%nExpecting:%n <%s>%nto have not failed:%nbut context failed to start" ,
362
- getApplicationContext ()));
358
+ throwAssertionError (contextFailedToStartWhenExpecting ("to have not failed" ));
363
359
}
364
360
return this ;
365
361
}
@@ -372,4 +368,61 @@ protected final Throwable getStartupFailure() {
372
368
return this .startupFailure ;
373
369
}
374
370
371
+ private ContextFailedToStart <C > contextFailedToStartWhenExpecting (
372
+ String expectationFormat , Object ... arguments ) {
373
+ return new ContextFailedToStart <C >(getApplicationContext (), this .startupFailure ,
374
+ expectationFormat , arguments );
375
+ }
376
+
377
+ private static final class ContextFailedToStart <C extends ApplicationContext >
378
+ extends BasicErrorMessageFactory {
379
+
380
+ private ContextFailedToStart (C context , Throwable ex , String expectationFormat ,
381
+ Object ... arguments ) {
382
+ super ("%nExpecting:%n <%s>%n" + expectationFormat
383
+ + ":%nbut context failed to start:%n%s" ,
384
+ combineArguments (context .toString (), ex , arguments ));
385
+ }
386
+
387
+ private static Object [] combineArguments (String context , Throwable ex ,
388
+ Object [] arguments ) {
389
+ Object [] combinedArguments = new Object [arguments .length + 2 ];
390
+ combinedArguments [0 ] = unquotedString (context );
391
+ System .arraycopy (arguments , 0 , combinedArguments , 1 , arguments .length );
392
+ combinedArguments [combinedArguments .length - 1 ] = unquotedString (
393
+ getIndentedStackTraceAsString (ex ));
394
+ return combinedArguments ;
395
+ }
396
+
397
+ private static String getIndentedStackTraceAsString (Throwable ex ) {
398
+ String stackTrace = getStackTraceAsString (ex );
399
+ return indent (stackTrace );
400
+ }
401
+
402
+ private static String getStackTraceAsString (Throwable ex ) {
403
+ StringWriter writer = new StringWriter ();
404
+ PrintWriter printer = new PrintWriter (writer );
405
+ ex .printStackTrace (printer );
406
+ return writer .toString ();
407
+ }
408
+
409
+ private static String indent (String input ) {
410
+ BufferedReader reader = new BufferedReader (new StringReader (input ));
411
+ StringWriter writer = new StringWriter ();
412
+ PrintWriter printer = new PrintWriter (writer );
413
+ try {
414
+ String line ;
415
+ while ((line = reader .readLine ()) != null ) {
416
+ printer .print (" " );
417
+ printer .println (line );
418
+ }
419
+ return writer .toString ();
420
+ }
421
+ catch (IOException ex ) {
422
+ return input ;
423
+ }
424
+ }
425
+
426
+ }
427
+
375
428
}
0 commit comments