Skip to content

Commit c7015ee

Browse files
authored
Merge pull request eugenp#5727 from rozagerardo/geroza/BAEL-9523_migrate-modules-to-parent-boot-2
[BAEL-9524] spring-boot | migrating to parent-boot-2 - further fixes
2 parents 7de531a + 31825c7 commit c7015ee

4 files changed

Lines changed: 88 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.springframework.boot.diagnostics.FailureAnalyzer=com.baeldung.failureanalyzer.MyBeanNotOfRequiredTypeFailureAnalyzer
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.failureanalyzer;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.Collection;
6+
import java.util.stream.Collectors;
7+
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
import org.springframework.beans.factory.BeanCreationException;
11+
import org.springframework.boot.SpringApplication;
12+
13+
import com.baeldung.failureanalyzer.utils.ListAppender;
14+
15+
import ch.qos.logback.classic.spi.ILoggingEvent;
16+
17+
public class FailureAnalyzerAppIntegrationTest {
18+
19+
private static final String EXPECTED_ANALYSIS_DESCRIPTION_TITLE = "Description:";
20+
private static final String EXPECTED_ANALYSIS_DESCRIPTION_CONTENT = "The bean myDAO could not be injected as com.baeldung.failureanalyzer.MyDAO because it is of type com.baeldung.failureanalyzer.MySecondDAO";
21+
private static final String EXPECTED_ANALYSIS_ACTION_TITLE = "Action:";
22+
private static final String EXPECTED_ANALYSIS_ACTION_CONTENT = "Consider creating a bean with name myDAO of type com.baeldung.failureanalyzer.MyDAO";
23+
24+
@BeforeEach
25+
public void clearLogList() {
26+
ListAppender.clearEventList();
27+
}
28+
29+
@Test
30+
public void givenBeanCreationErrorInContext_whenContextLoaded_thenFailureAnalyzerLogsReport() {
31+
try {
32+
SpringApplication.run(FailureAnalyzerApplication.class);
33+
} catch (BeanCreationException e) {
34+
Collection<String> allLoggedEntries = ListAppender.getEvents()
35+
.stream()
36+
.map(ILoggingEvent::getFormattedMessage)
37+
.collect(Collectors.toList());
38+
assertThat(allLoggedEntries).anyMatch(entry -> entry.contains(EXPECTED_ANALYSIS_DESCRIPTION_TITLE))
39+
.anyMatch(entry -> entry.contains(EXPECTED_ANALYSIS_DESCRIPTION_CONTENT))
40+
.anyMatch(entry -> entry.contains(EXPECTED_ANALYSIS_ACTION_TITLE))
41+
.anyMatch(entry -> entry.contains(EXPECTED_ANALYSIS_ACTION_CONTENT));
42+
return;
43+
}
44+
throw new IllegalStateException("Context load should be failing due to a BeanCreationException!");
45+
}
46+
47+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.failureanalyzer.utils;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import ch.qos.logback.classic.spi.ILoggingEvent;
7+
import ch.qos.logback.core.AppenderBase;
8+
9+
public class ListAppender extends AppenderBase<ILoggingEvent> {
10+
11+
static private List<ILoggingEvent> events = new ArrayList<>();
12+
13+
@Override
14+
protected void append(ILoggingEvent eventObject) {
15+
events.add(eventObject);
16+
}
17+
18+
public static List<ILoggingEvent> getEvents() {
19+
return events;
20+
}
21+
22+
public static void clearEventList() {
23+
events.clear();
24+
}
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<include
4+
resource="org/springframework/boot/logging/logback/base.xml" />
5+
<appender name="LISTAPPENDER"
6+
class="com.baeldung.failureanalyzer.utils.ListAppender">
7+
</appender>
8+
<logger
9+
name="org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter">
10+
<appender-ref ref="LISTAPPENDER" />
11+
</logger>
12+
<root level="info">
13+
<appender-ref ref="CONSOLE" />
14+
</root>
15+
</configuration>

0 commit comments

Comments
 (0)