|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2024 the original author or authors. |
| 2 | + * Copyright 2012-2025 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.test.autoconfigure;
|
18 | 18 |
|
| 19 | +import org.junit.jupiter.api.BeforeEach; |
19 | 20 | import org.junit.jupiter.api.Test;
|
20 | 21 | import org.junit.jupiter.api.extension.ExtendWith;
|
21 | 22 |
|
|
24 | 25 | import org.springframework.boot.test.context.SpringBootTest;
|
25 | 26 | import org.springframework.boot.test.system.CapturedOutput;
|
26 | 27 | import org.springframework.boot.test.system.OutputCaptureExtension;
|
| 28 | +import org.springframework.boot.testsupport.classpath.resources.WithResource; |
27 | 29 | import org.springframework.context.annotation.Bean;
|
28 | 30 | import org.springframework.context.annotation.Configuration;
|
29 | 31 | 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; |
30 | 35 |
|
31 | 36 | import static org.assertj.core.api.Assertions.assertThat;
|
32 | 37 | import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
|
39 | 44 | @ExtendWith(OutputCaptureExtension.class)
|
40 | 45 | class OnFailureConditionReportContextCustomizerFactoryTests {
|
41 | 46 |
|
| 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 | + |
42 | 56 | @Test
|
43 | 57 | 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() { |
44 | 81 | assertThatIllegalStateException()
|
45 | 82 | .isThrownBy(() -> new TestContextManager(FailingTests.class).getTestContext().getApplicationContext());
|
46 |
| - assertThat(output).contains("JacksonAutoConfiguration matched"); |
47 | 83 | }
|
48 | 84 |
|
49 | 85 | @SpringBootTest
|
|
0 commit comments