|
| 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 | +} |
0 commit comments