Search in sources :

Example 1 with CapturedOutput

use of org.springframework.boot.testsupport.system.CapturedOutput in project spring-boot by spring-projects.

the class SpringApplicationTests method runnersAreCalledAfterStartedIsLoggedAndBeforeApplicationReadyEventIsPublished.

@Test
@SuppressWarnings("unchecked")
void runnersAreCalledAfterStartedIsLoggedAndBeforeApplicationReadyEventIsPublished(CapturedOutput output) throws Exception {
    SpringApplication application = new SpringApplication(ExampleConfig.class);
    ApplicationRunner applicationRunner = mock(ApplicationRunner.class);
    CommandLineRunner commandLineRunner = mock(CommandLineRunner.class);
    application.addInitializers((context) -> {
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        beanFactory.registerSingleton("commandLineRunner", (CommandLineRunner) (args) -> {
            assertThat(output).contains("Started");
            commandLineRunner.run(args);
        });
        beanFactory.registerSingleton("applicationRunner", (ApplicationRunner) (args) -> {
            assertThat(output).contains("Started");
            applicationRunner.run(args);
        });
    });
    application.setWebApplicationType(WebApplicationType.NONE);
    ApplicationListener<ApplicationReadyEvent> eventListener = mock(ApplicationListener.class);
    application.addListeners(eventListener);
    this.context = application.run();
    InOrder applicationRunnerOrder = Mockito.inOrder(eventListener, applicationRunner);
    applicationRunnerOrder.verify(applicationRunner).run(any(ApplicationArguments.class));
    applicationRunnerOrder.verify(eventListener).onApplicationEvent(any(ApplicationReadyEvent.class));
    InOrder commandLineRunnerOrder = Mockito.inOrder(eventListener, commandLineRunner);
    commandLineRunnerOrder.verify(commandLineRunner).run();
    commandLineRunnerOrder.verify(eventListener).onApplicationEvent(any(ApplicationReadyEvent.class));
}
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) InOrder(org.mockito.InOrder) ApplicationReadyEvent(org.springframework.boot.context.event.ApplicationReadyEvent) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) Test(org.junit.jupiter.api.Test)

Example 2 with CapturedOutput

use of org.springframework.boot.testsupport.system.CapturedOutput in project spring-boot by spring-projects.

the class ErrorPageFilterTests method errorMessageForRequestWithPathInfo.

@Test
void errorMessageForRequestWithPathInfo(CapturedOutput output) throws IOException, ServletException {
    this.request.setServletPath("/test");
    this.request.setPathInfo("/alpha");
    this.filter.addErrorPages(new ErrorPage("/error"));
    this.chain = new TestFilterChain((request, response, chain) -> {
        chain.call();
        throw new RuntimeException();
    });
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(output).contains("request [/test/alpha]");
}
Also used : RequestDispatcher(jakarta.servlet.RequestDispatcher) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockFilterChain(org.springframework.mock.web.MockFilterChain) Enumeration(java.util.Enumeration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DeferredResult(org.springframework.web.context.request.async.DeferredResult) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ClientAbortException(org.apache.catalina.connector.ClientAbortException) ServletException(jakarta.servlet.ServletException) WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) OutputCaptureExtension(org.springframework.boot.testsupport.system.OutputCaptureExtension) BDDMockito.given(org.mockito.BDDMockito.given) NestedServletException(org.springframework.web.util.NestedServletException) Map(java.util.Map) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) ServletRequest(jakarta.servlet.ServletRequest) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) WebAsyncUtils(org.springframework.web.context.request.async.WebAsyncUtils) ErrorPage(org.springframework.boot.web.server.ErrorPage) MockRequestDispatcher(org.springframework.mock.web.MockRequestDispatcher) BDDMockito.then(org.mockito.BDDMockito.then) IOException(java.io.IOException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) HttpStatus(org.springframework.http.HttpStatus) Mockito.never(org.mockito.Mockito.never) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) CapturedOutput(org.springframework.boot.testsupport.system.CapturedOutput) Mockito.mock(org.mockito.Mockito.mock) ErrorPage(org.springframework.boot.web.server.ErrorPage) Test(org.junit.jupiter.api.Test)

Example 3 with CapturedOutput

use of org.springframework.boot.testsupport.system.CapturedOutput in project spring-boot by spring-projects.

the class ErrorPageFilterTests method responseCommittedWhenFromClientAbortException.

@Test
void responseCommittedWhenFromClientAbortException(CapturedOutput output) throws Exception {
    this.filter.addErrorPages(new ErrorPage("/error"));
    this.response.setCommitted(true);
    this.chain = new TestFilterChain((request, response, chain) -> {
        chain.call();
        throw new ClientAbortException();
    });
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(this.response.isCommitted()).isTrue();
    assertThat(output).doesNotContain("Cannot forward");
}
Also used : RequestDispatcher(jakarta.servlet.RequestDispatcher) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockFilterChain(org.springframework.mock.web.MockFilterChain) Enumeration(java.util.Enumeration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DeferredResult(org.springframework.web.context.request.async.DeferredResult) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ClientAbortException(org.apache.catalina.connector.ClientAbortException) ServletException(jakarta.servlet.ServletException) WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) OutputCaptureExtension(org.springframework.boot.testsupport.system.OutputCaptureExtension) BDDMockito.given(org.mockito.BDDMockito.given) NestedServletException(org.springframework.web.util.NestedServletException) Map(java.util.Map) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) ServletRequest(jakarta.servlet.ServletRequest) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) WebAsyncUtils(org.springframework.web.context.request.async.WebAsyncUtils) ErrorPage(org.springframework.boot.web.server.ErrorPage) MockRequestDispatcher(org.springframework.mock.web.MockRequestDispatcher) BDDMockito.then(org.mockito.BDDMockito.then) IOException(java.io.IOException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) HttpStatus(org.springframework.http.HttpStatus) Mockito.never(org.mockito.Mockito.never) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) CapturedOutput(org.springframework.boot.testsupport.system.CapturedOutput) Mockito.mock(org.mockito.Mockito.mock) ErrorPage(org.springframework.boot.web.server.ErrorPage) ClientAbortException(org.apache.catalina.connector.ClientAbortException) Test(org.junit.jupiter.api.Test)

Example 4 with CapturedOutput

use of org.springframework.boot.testsupport.system.CapturedOutput in project spring-boot by spring-projects.

the class ErrorPageFilterTests method errorMessageForRequestWithoutPathInfo.

@Test
void errorMessageForRequestWithoutPathInfo(CapturedOutput output) throws IOException, ServletException {
    this.request.setServletPath("/test");
    this.filter.addErrorPages(new ErrorPage("/error"));
    this.chain = new TestFilterChain((request, response, chain) -> {
        chain.call();
        throw new RuntimeException();
    });
    this.filter.doFilter(this.request, this.response, this.chain);
    assertThat(output).contains("request [/test]");
}
Also used : RequestDispatcher(jakarta.servlet.RequestDispatcher) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockFilterChain(org.springframework.mock.web.MockFilterChain) Enumeration(java.util.Enumeration) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DeferredResult(org.springframework.web.context.request.async.DeferredResult) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ClientAbortException(org.apache.catalina.connector.ClientAbortException) ServletException(jakarta.servlet.ServletException) WebAsyncManager(org.springframework.web.context.request.async.WebAsyncManager) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) OutputCaptureExtension(org.springframework.boot.testsupport.system.OutputCaptureExtension) BDDMockito.given(org.mockito.BDDMockito.given) NestedServletException(org.springframework.web.util.NestedServletException) Map(java.util.Map) StandardServletAsyncWebRequest(org.springframework.web.context.request.async.StandardServletAsyncWebRequest) ServletRequest(jakarta.servlet.ServletRequest) MissingServletRequestParameterException(org.springframework.web.bind.MissingServletRequestParameterException) WebAsyncUtils(org.springframework.web.context.request.async.WebAsyncUtils) ErrorPage(org.springframework.boot.web.server.ErrorPage) MockRequestDispatcher(org.springframework.mock.web.MockRequestDispatcher) BDDMockito.then(org.mockito.BDDMockito.then) IOException(java.io.IOException) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Test(org.junit.jupiter.api.Test) HttpServletResponseWrapper(jakarta.servlet.http.HttpServletResponseWrapper) HttpStatus(org.springframework.http.HttpStatus) Mockito.never(org.mockito.Mockito.never) ServletResponse(jakarta.servlet.ServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) CapturedOutput(org.springframework.boot.testsupport.system.CapturedOutput) Mockito.mock(org.mockito.Mockito.mock) ErrorPage(org.springframework.boot.web.server.ErrorPage) Test(org.junit.jupiter.api.Test)

Aggregations

HashMap (java.util.HashMap)4 Map (java.util.Map)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Test (org.junit.jupiter.api.Test)4 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)4 BDDMockito.given (org.mockito.BDDMockito.given)4 BDDMockito.then (org.mockito.BDDMockito.then)4 Mockito.mock (org.mockito.Mockito.mock)4 Mockito.never (org.mockito.Mockito.never)4 CapturedOutput (org.springframework.boot.testsupport.system.CapturedOutput)4 OutputCaptureExtension (org.springframework.boot.testsupport.system.OutputCaptureExtension)4 RequestDispatcher (jakarta.servlet.RequestDispatcher)3 ServletException (jakarta.servlet.ServletException)3 ServletRequest (jakarta.servlet.ServletRequest)3 ServletResponse (jakarta.servlet.ServletResponse)3 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)3 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)3 HttpServletResponseWrapper (jakarta.servlet.http.HttpServletResponseWrapper)3 IOException (java.io.IOException)3 Enumeration (java.util.Enumeration)3