Search in sources :

Example 6 with ApplicationFailedEvent

use of org.springframework.boot.context.event.ApplicationFailedEvent in project spring-boot by spring-projects.

the class ConditionEvaluationReportLoggingListenerTests method logsDebugOnError.

@Test
void logsDebugOnError(CapturedOutput output) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    this.initializer.initialize(context);
    context.register(ErrorConfig.class);
    assertThatExceptionOfType(Exception.class).isThrownBy(context::refresh).satisfies((ex) -> withDebugLogging(() -> this.initializer.onApplicationEvent(new ApplicationFailedEvent(new SpringApplication(), new String[0], context, ex))));
    assertThat(output).contains("CONDITIONS EVALUATION REPORT");
}
Also used : ApplicationFailedEvent(org.springframework.boot.context.event.ApplicationFailedEvent) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) SpringApplication(org.springframework.boot.SpringApplication) Test(org.junit.jupiter.api.Test)

Example 7 with ApplicationFailedEvent

use of org.springframework.boot.context.event.ApplicationFailedEvent in project spring-boot by spring-projects.

the class SpringApplicationTests method deregistersShutdownHookForFailedApplicationContext.

@Test
void deregistersShutdownHookForFailedApplicationContext() {
    SpringApplication application = new SpringApplication(BrokenPostConstructConfig.class);
    List<ApplicationEvent> events = new ArrayList<>();
    application.addListeners(events::add);
    application.setWebApplicationType(WebApplicationType.NONE);
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(application::run);
    assertThat(events).hasAtLeastOneElementOfType(ApplicationFailedEvent.class);
    ApplicationFailedEvent failure = events.stream().filter((event) -> event instanceof ApplicationFailedEvent).map(ApplicationFailedEvent.class::cast).findFirst().get();
    assertThat(SpringApplicationShutdownHookInstance.get()).didNotRegisterApplicationContext(failure.getApplicationContext());
}
Also used : ApplicationFailedEvent(org.springframework.boot.context.event.ApplicationFailedEvent) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers(org.mockito.ArgumentMatchers) ApplicationReadyEvent(org.springframework.boot.context.event.ApplicationReadyEvent) Autowired(org.springframework.beans.factory.annotation.Autowired) ApplicationContextException(org.springframework.context.ApplicationContextException) AnnotationConfigUtils(org.springframework.context.annotation.AnnotationConfigUtils) SimpleApplicationEventMulticaster(org.springframework.context.event.SimpleApplicationEventMulticaster) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assertions.assertThatNoException(org.assertj.core.api.Assertions.assertThatNoException) BDDMockito.given(org.mockito.BDDMockito.given) ApplicationStartedEvent(org.springframework.boot.context.event.ApplicationStartedEvent) Map(java.util.Map) AvailabilityChangeEvent(org.springframework.boot.availability.AvailabilityChangeEvent) TomcatServletWebServerFactory(org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory) Resource(org.springframework.core.io.Resource) ApplicationContextInitializedEvent(org.springframework.boot.context.event.ApplicationContextInitializedEvent) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Set(java.util.Set) CachedIntrospectionResults(org.springframework.beans.CachedIntrospectionResults) ApplicationStartup(org.springframework.core.metrics.ApplicationStartup) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ApplicationStartingEvent(org.springframework.boot.context.event.ApplicationStartingEvent) CompositePropertySource(org.springframework.core.env.CompositePropertySource) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) BeanNameGenerator(org.springframework.beans.factory.support.BeanNameGenerator) Lazy(org.springframework.context.annotation.Lazy) ApplicationContextAware(org.springframework.context.ApplicationContextAware) Mockito.mock(org.mockito.Mockito.mock) Ordered(org.springframework.core.Ordered) CommandLinePropertySource(org.springframework.core.env.CommandLinePropertySource) PropertySource(org.springframework.core.env.PropertySource) Mockito.spy(org.mockito.Mockito.spy) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) ApplicationEnvironmentPreparedEvent(org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) BeanCreationException(org.springframework.beans.factory.BeanCreationException) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) LinkedHashSet(java.util.LinkedHashSet) ArgumentMatchers.isA(org.mockito.ArgumentMatchers.isA) ReactiveWebApplicationContext(org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext) LivenessState(org.springframework.boot.availability.LivenessState) SmartApplicationListener(org.springframework.context.event.SmartApplicationListener) MultiValueMap(org.springframework.util.MultiValueMap) Mono(reactor.core.publisher.Mono) ApplicationEvent(org.springframework.context.ApplicationEvent) ConfigurableWebEnvironment(org.springframework.web.context.ConfigurableWebEnvironment) AfterEach(org.junit.jupiter.api.AfterEach) Mockito.never(org.mockito.Mockito.never) NettyReactiveWebServerFactory(org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory) Condition(org.assertj.core.api.Condition) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) CapturedOutput(org.springframework.boot.testsupport.system.CapturedOutput) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MockEnvironment(org.springframework.mock.env.MockEnvironment) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AnnotationConfigServletWebServerApplicationContext(org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext) Mockito.mockingDetails(org.mockito.Mockito.mockingDetails) ApplicationPreparedEvent(org.springframework.boot.context.event.ApplicationPreparedEvent) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BeanDefinitionRegistry(org.springframework.beans.factory.support.BeanDefinitionRegistry) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) DefaultBeanNameGenerator(org.springframework.beans.factory.support.DefaultBeanNameGenerator) ArgumentMatcher(org.mockito.ArgumentMatcher) BeanCurrentlyInCreationException(org.springframework.beans.factory.BeanCurrentlyInCreationException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Profiles(org.springframework.core.env.Profiles) ApplicationEventMulticaster(org.springframework.context.event.ApplicationEventMulticaster) ResourceLoader(org.springframework.core.io.ResourceLoader) BDDMockito.willThrow(org.mockito.BDDMockito.willThrow) ApplicationConversionService(org.springframework.boot.convert.ApplicationConversionService) StandardServletEnvironment(org.springframework.web.context.support.StandardServletEnvironment) WebApplicationContext(org.springframework.web.context.WebApplicationContext) ApplicationListener(org.springframework.context.ApplicationListener) ApplicationFailedEvent(org.springframework.boot.context.event.ApplicationFailedEvent) BeanDefinitionOverrideException(org.springframework.beans.factory.support.BeanDefinitionOverrideException) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) List(java.util.List) InstanceSupplier(org.springframework.boot.BootstrapRegistry.InstanceSupplier) TestPropertySourceUtils(org.springframework.test.context.support.TestPropertySourceUtils) Environment(org.springframework.core.env.Environment) MapPropertySource(org.springframework.core.env.MapPropertySource) PostConstruct(javax.annotation.PostConstruct) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AnnotationConfigReactiveWebServerApplicationContext(org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext) ClassPathResource(org.springframework.core.io.ClassPathResource) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) UnsatisfiedDependencyException(org.springframework.beans.factory.UnsatisfiedDependencyException) ObjectProvider(org.springframework.beans.factory.ObjectProvider) ArgumentCaptor(org.mockito.ArgumentCaptor) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) OutputCaptureExtension(org.springframework.boot.testsupport.system.OutputCaptureExtension) DefaultResourceLoader(org.springframework.core.io.DefaultResourceLoader) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) ReadinessState(org.springframework.boot.availability.ReadinessState) InOrder(org.mockito.InOrder) Iterator(java.util.Iterator) BDDMockito.then(org.mockito.BDDMockito.then) StandardEnvironment(org.springframework.core.env.StandardEnvironment) AvailabilityState(org.springframework.boot.availability.AvailabilityState) StartupStep(org.springframework.core.metrics.StartupStep) ApplicationContext(org.springframework.context.ApplicationContext) SpringApplicationEvent(org.springframework.boot.context.event.SpringApplicationEvent) Mockito(org.mockito.Mockito) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) HttpHandler(org.springframework.http.server.reactive.HttpHandler) Bean(org.springframework.context.annotation.Bean) Collections(java.util.Collections) ApplicationContextInitializer(org.springframework.context.ApplicationContextInitializer) StringUtils(org.springframework.util.StringUtils) BeanCreationException(org.springframework.beans.factory.BeanCreationException) ApplicationEvent(org.springframework.context.ApplicationEvent) SpringApplicationEvent(org.springframework.boot.context.event.SpringApplicationEvent) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 8 with ApplicationFailedEvent

use of org.springframework.boot.context.event.ApplicationFailedEvent in project spring-boot by spring-projects.

the class EnvironmentPostProcessorApplicationListenerTests method onApplicationEventWhenApplicationFailedEventSwitchesLogs.

@Test
void onApplicationEventWhenApplicationFailedEventSwitchesLogs() {
    SpringApplication application = mock(SpringApplication.class);
    ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
    ApplicationFailedEvent event = new ApplicationFailedEvent(application, new String[0], context, new RuntimeException());
    this.listener.onApplicationEvent(event);
    then(this.deferredLogs).should().switchOverAll();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) ApplicationFailedEvent(org.springframework.boot.context.event.ApplicationFailedEvent) SpringApplication(org.springframework.boot.SpringApplication) Test(org.junit.jupiter.api.Test)

Example 9 with ApplicationFailedEvent

use of org.springframework.boot.context.event.ApplicationFailedEvent in project spring-boot by spring-projects.

the class AutoConfigurationReportLoggingInitializerTests method logsDebugOnError.

@Test
public void logsDebugOnError() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    this.initializer.initialize(context);
    context.register(ErrorConfig.class);
    try {
        context.refresh();
        fail("Did not error");
    } catch (Exception ex) {
        this.initializer.onApplicationEvent(new ApplicationFailedEvent(new SpringApplication(), new String[0], context, ex));
    }
    assertThat(this.debugLog.size()).isNotEqualTo(0);
    assertThat(this.infoLog.size()).isEqualTo(0);
}
Also used : ApplicationFailedEvent(org.springframework.boot.context.event.ApplicationFailedEvent) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) SpringApplication(org.springframework.boot.SpringApplication) LogConfigurationException(org.apache.commons.logging.LogConfigurationException) Test(org.junit.Test)

Example 10 with ApplicationFailedEvent

use of org.springframework.boot.context.event.ApplicationFailedEvent in project spring-boot by spring-projects.

the class AutoConfigurationReportLoggingInitializerTests method noErrorIfNotInitialized.

@Test
public void noErrorIfNotInitialized() throws Exception {
    this.initializer.onApplicationEvent(new ApplicationFailedEvent(new SpringApplication(), new String[0], null, new RuntimeException("Planned")));
    assertThat(this.infoLog.get(0)).contains("Unable to provide auto-configuration report");
}
Also used : ApplicationFailedEvent(org.springframework.boot.context.event.ApplicationFailedEvent) SpringApplication(org.springframework.boot.SpringApplication) Test(org.junit.Test)

Aggregations

ApplicationFailedEvent (org.springframework.boot.context.event.ApplicationFailedEvent)13 SpringApplication (org.springframework.boot.SpringApplication)9 Test (org.junit.jupiter.api.Test)6 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)5 Test (org.junit.Test)4 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)4 ApplicationStartingEvent (org.springframework.boot.context.event.ApplicationStartingEvent)3 IOException (java.io.IOException)2 LogConfigurationException (org.apache.commons.logging.LogConfigurationException)2 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)2 ApplicationEnvironmentPreparedEvent (org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent)2 ApplicationStartedEvent (org.springframework.boot.context.event.ApplicationStartedEvent)2 AnnotationConfigServletWebServerApplicationContext (org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext)2 ApplicationContext (org.springframework.context.ApplicationContext)2 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedHashSet (java.util.LinkedHashSet)1