Skip to content

Commit 1d67e52

Browse files
committed
Merge pull request #45268 from nosan
* pr/45268: Polish 'Introduce 'spring.test.print-condition-evaluation-report' property' Introduce 'spring.test.print-condition-evaluation-report' property Closes gh-45268
2 parents e50da4f + 848494d commit 1d67e52

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/OnFailureConditionReportContextCustomizerFactory.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -84,7 +84,14 @@ private ApplicationFailureListener(Supplier<ConditionEvaluationReport> reportSup
8484

8585
@Override
8686
public void onApplicationEvent(ApplicationFailedEvent event) {
87-
System.err.println(new ConditionEvaluationReportMessage(this.reportSupplier.get()));
87+
if (shouldPrintReport(event.getApplicationContext())) {
88+
System.err.println(new ConditionEvaluationReportMessage(this.reportSupplier.get()));
89+
}
90+
}
91+
92+
private static boolean shouldPrintReport(ConfigurableApplicationContext context) {
93+
return (context == null) || context.getEnvironment()
94+
.getProperty("spring.test.print-condition-evaluation-report", Boolean.class, true);
8895
}
8996

9097
}

spring-boot-project/spring-boot-test-autoconfigure/src/main/resources/META-INF/spring-configuration-metadata.json

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
"type": "java.lang.Boolean",
1818
"description": "Whether observability should be auto-configured in tests.",
1919
"defaultValue": "false"
20+
},
21+
{
22+
"name": "spring.test.print-condition-evaluation-report",
23+
"type": "java.lang.Boolean",
24+
"description": "Whether the condition evaluation report should be printed when the ApplicationContext fails to start.",
25+
"defaultValue": "true"
2026
}
2127
]
2228
}

spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/OnFailureConditionReportContextCustomizerFactoryTests.java

+38-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.test.autoconfigure;
1818

19+
import org.junit.jupiter.api.BeforeEach;
1920
import org.junit.jupiter.api.Test;
2021
import org.junit.jupiter.api.extension.ExtendWith;
2122

@@ -24,9 +25,13 @@
2425
import org.springframework.boot.test.context.SpringBootTest;
2526
import org.springframework.boot.test.system.CapturedOutput;
2627
import org.springframework.boot.test.system.OutputCaptureExtension;
28+
import org.springframework.boot.testsupport.classpath.resources.WithResource;
2729
import org.springframework.context.annotation.Bean;
2830
import org.springframework.context.annotation.Configuration;
2931
import org.springframework.test.context.TestContextManager;
32+
import org.springframework.test.context.cache.ContextCache;
33+
import org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate;
34+
import org.springframework.test.util.ReflectionTestUtils;
3035

3136
import static org.assertj.core.api.Assertions.assertThat;
3237
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
@@ -39,11 +44,42 @@
3944
@ExtendWith(OutputCaptureExtension.class)
4045
class OnFailureConditionReportContextCustomizerFactoryTests {
4146

47+
@BeforeEach
48+
void clearCache() {
49+
ContextCache contextCache = (ContextCache) ReflectionTestUtils
50+
.getField(DefaultCacheAwareContextLoaderDelegate.class, "defaultContextCache");
51+
if (contextCache != null) {
52+
contextCache.reset();
53+
}
54+
}
55+
4256
@Test
4357
void loadFailureShouldPrintReport(CapturedOutput output) {
58+
load();
59+
assertThat(output.getErr()).contains("JacksonAutoConfiguration matched");
60+
assertThat(output).contains("Error creating bean with name 'faultyBean'");
61+
}
62+
63+
@Test
64+
@WithResource(name = "application.xml", content = "invalid xml")
65+
void loadFailureShouldNotPrintReportWhenApplicationPropertiesIsBroken(CapturedOutput output) {
66+
load();
67+
assertThat(output).doesNotContain("JacksonAutoConfiguration matched")
68+
.doesNotContain("Error creating bean with name 'faultyBean'")
69+
.contains("java.util.InvalidPropertiesFormatException");
70+
}
71+
72+
@Test
73+
@WithResource(name = "application.properties", content = "spring.test.print-condition-evaluation-report=false")
74+
void loadFailureShouldNotPrintReportWhenDisabled(CapturedOutput output) {
75+
load();
76+
assertThat(output).doesNotContain("JacksonAutoConfiguration matched")
77+
.contains("Error creating bean with name 'faultyBean'");
78+
}
79+
80+
private void load() {
4481
assertThatIllegalStateException()
4582
.isThrownBy(() -> new TestContextManager(FailingTests.class).getTestContext().getApplicationContext());
46-
assertThat(output).contains("JacksonAutoConfiguration matched");
4783
}
4884

4985
@SpringBootTest

0 commit comments

Comments
 (0)