Search in sources :

Example 1 with ApplicationStartup

use of org.springframework.core.metrics.ApplicationStartup in project spring-boot by spring-projects.

the class SpringApplicationTests method customApplicationStartupPublishStartupStepsWithFailure.

@Test
void customApplicationStartupPublishStartupStepsWithFailure() {
    ApplicationStartup applicationStartup = mock(ApplicationStartup.class);
    StartupStep startupStep = mock(StartupStep.class);
    given(applicationStartup.start(anyString())).willReturn(startupStep);
    given(startupStep.tag(anyString(), anyString())).willReturn(startupStep);
    given(startupStep.tag(anyString(), ArgumentMatchers.<Supplier<String>>any())).willReturn(startupStep);
    SpringApplication application = new SpringApplication(BrokenPostConstructConfig.class);
    application.setWebApplicationType(WebApplicationType.NONE);
    application.setApplicationStartup(applicationStartup);
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(application::run);
    then(applicationStartup).should().start("spring.boot.application.starting");
    then(applicationStartup).should().start("spring.boot.application.environment-prepared");
    then(applicationStartup).should().start("spring.boot.application.failed");
    long startCount = mockingDetails(applicationStartup).getInvocations().stream().filter((invocation) -> invocation.getMethod().toString().contains("start(")).count();
    long endCount = mockingDetails(startupStep).getInvocations().stream().filter((invocation) -> invocation.getMethod().toString().contains("end(")).count();
    assertThat(startCount).isEqualTo(endCount);
}
Also used : 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) StartupStep(org.springframework.core.metrics.StartupStep) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ApplicationStartup(org.springframework.core.metrics.ApplicationStartup) Test(org.junit.jupiter.api.Test)

Example 2 with ApplicationStartup

use of org.springframework.core.metrics.ApplicationStartup in project spring-boot by spring-projects.

the class SpringApplicationTests method customApplicationStartupPublishStartupSteps.

@Test
void customApplicationStartupPublishStartupSteps() {
    ApplicationStartup applicationStartup = mock(ApplicationStartup.class);
    StartupStep startupStep = mock(StartupStep.class);
    given(applicationStartup.start(anyString())).willReturn(startupStep);
    given(startupStep.tag(anyString(), anyString())).willReturn(startupStep);
    given(startupStep.tag(anyString(), ArgumentMatchers.<Supplier<String>>any())).willReturn(startupStep);
    SpringApplication application = new SpringApplication(ExampleConfig.class);
    application.setWebApplicationType(WebApplicationType.NONE);
    application.setApplicationStartup(applicationStartup);
    this.context = application.run();
    assertThat(this.context.getBean(ApplicationStartup.class)).isEqualTo(applicationStartup);
    then(applicationStartup).should().start("spring.boot.application.starting");
    then(applicationStartup).should().start("spring.boot.application.environment-prepared");
    then(applicationStartup).should().start("spring.boot.application.context-prepared");
    then(applicationStartup).should().start("spring.boot.application.context-loaded");
    then(applicationStartup).should().start("spring.boot.application.started");
    then(applicationStartup).should().start("spring.boot.application.ready");
    long startCount = mockingDetails(applicationStartup).getInvocations().stream().filter((invocation) -> invocation.getMethod().toString().contains("start(")).count();
    long endCount = mockingDetails(startupStep).getInvocations().stream().filter((invocation) -> invocation.getMethod().toString().contains("end(")).count();
    assertThat(startCount).isEqualTo(endCount);
}
Also used : 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) StartupStep(org.springframework.core.metrics.StartupStep) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ApplicationStartup(org.springframework.core.metrics.ApplicationStartup) Test(org.junit.jupiter.api.Test)

Example 3 with ApplicationStartup

use of org.springframework.core.metrics.ApplicationStartup in project spring-boot by spring-projects.

the class StartupEndpointTests method testStartupEndpoint.

private void testStartupEndpoint(ApplicationStartup applicationStartup, Consumer<StartupEndpoint> startupEndpoint) {
    ApplicationContextRunner contextRunner = new ApplicationContextRunner().withInitializer((context) -> context.setApplicationStartup(applicationStartup)).withUserConfiguration(EndpointConfiguration.class);
    contextRunner.run((context) -> {
        assertThat(context).hasSingleBean(StartupEndpoint.class);
        startupEndpoint.accept(context.getBean(StartupEndpoint.class));
    });
}
Also used : Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Configuration(org.springframework.context.annotation.Configuration) SpringBootVersion(org.springframework.boot.SpringBootVersion) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BufferingApplicationStartup(org.springframework.boot.context.metrics.buffering.BufferingApplicationStartup) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner) Bean(org.springframework.context.annotation.Bean) StartupResponse(org.springframework.boot.actuate.startup.StartupEndpoint.StartupResponse) ApplicationStartup(org.springframework.core.metrics.ApplicationStartup) ApplicationContextRunner(org.springframework.boot.test.context.runner.ApplicationContextRunner)

Aggregations

Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Test (org.junit.jupiter.api.Test)3 Bean (org.springframework.context.annotation.Bean)3 Configuration (org.springframework.context.annotation.Configuration)3 ApplicationStartup (org.springframework.core.metrics.ApplicationStartup)3 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Supplier (java.util.function.Supplier)2 PostConstruct (javax.annotation.PostConstruct)2 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)2 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)2 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)2