diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfigurationTests.java index cfae6cd333..d1de8bc6e8 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/export/prometheus/PrometheusMetricsExportAutoConfigurationTests.java @@ -21,7 +21,7 @@ import io.micrometer.prometheus.PrometheusConfig; import io.micrometer.prometheus.PrometheusMeterRegistry; import io.prometheus.client.CollectorRegistry; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration; import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusPushGatewayManager; @@ -29,8 +29,8 @@ import org.springframework.boot.actuate.metrics.export.prometheus.PrometheusScra import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.assertj.AssertableApplicationContext; import org.springframework.boot.test.context.runner.ApplicationContextRunner; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -43,11 +43,9 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Andy Wilkinson */ +@ExtendWith(OutputCaptureExtension.class) public class PrometheusMetricsExportAutoConfigurationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations .of(PrometheusMetricsExportAutoConfiguration.class)); @@ -152,14 +150,14 @@ public class PrometheusMetricsExportAutoConfigurationTests { } @Test - public void withPushGatewayEnabled() { + public void withPushGatewayEnabled(CapturedOutput capturedOutput) { this.contextRunner .withConfiguration( AutoConfigurations.of(ManagementContextAutoConfiguration.class)) .withPropertyValues( "management.metrics.export.prometheus.pushgateway.enabled=true") .withUserConfiguration(BaseConfiguration.class).run((context) -> { - assertThat(this.output.toString()) + assertThat(capturedOutput) .doesNotContain("Invalid PushGateway base url"); hasGatewayURL(context, "http://localhost:9091/metrics/job/"); }); @@ -167,7 +165,7 @@ public class PrometheusMetricsExportAutoConfigurationTests { @Test @Deprecated - public void withCustomLegacyPushGatewayURL() { + public void withCustomLegacyPushGatewayURL(CapturedOutput capturedOutput) { this.contextRunner .withConfiguration( AutoConfigurations.of(ManagementContextAutoConfiguration.class)) @@ -175,8 +173,7 @@ public class PrometheusMetricsExportAutoConfigurationTests { "management.metrics.export.prometheus.pushgateway.enabled=true", "management.metrics.export.prometheus.pushgateway.base-url=localhost:9090") .withUserConfiguration(BaseConfiguration.class).run((context) -> { - assertThat(this.output.toString()) - .contains("Invalid PushGateway base url") + assertThat(capturedOutput).contains("Invalid PushGateway base url") .contains("localhost:9090"); hasGatewayURL(context, "http://localhost:9090/metrics/job/"); }); diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/client/RestTemplateMetricsConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/client/RestTemplateMetricsConfigurationTests.java index 1957439e5f..b3991f812e 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/client/RestTemplateMetricsConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/client/RestTemplateMetricsConfigurationTests.java @@ -20,7 +20,7 @@ import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.Timer; import io.micrometer.core.instrument.distribution.HistogramSnapshot; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun; import org.springframework.boot.actuate.metrics.web.client.DefaultRestTemplateExchangeTagsProvider; @@ -29,8 +29,8 @@ import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration; import org.springframework.boot.test.context.assertj.AssertableApplicationContext; import org.springframework.boot.test.context.runner.ApplicationContextRunner; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.http.HttpStatus; import org.springframework.test.web.client.MockRestServiceServer; @@ -47,6 +47,7 @@ import static org.springframework.test.web.client.response.MockRestResponseCreat * @author Jon Schneider * @author Raheela Aslam */ +@ExtendWith(OutputCaptureExtension.class) public class RestTemplateMetricsConfigurationTests { private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() @@ -54,9 +55,6 @@ public class RestTemplateMetricsConfigurationTests { .withConfiguration(AutoConfigurations.of(RestTemplateAutoConfiguration.class, HttpClientMetricsAutoConfiguration.class)); - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test public void restTemplateCreatedWithBuilderIsInstrumented() { this.contextRunner.run((context) -> { @@ -78,26 +76,26 @@ public class RestTemplateMetricsConfigurationTests { } @Test - public void afterMaxUrisReachedFurtherUrisAreDenied() { + public void afterMaxUrisReachedFurtherUrisAreDenied(CapturedOutput capturedOutput) { this.contextRunner .withPropertyValues("management.metrics.web.client.max-uri-tags=2") .run((context) -> { MeterRegistry registry = getInitializedMeterRegistry(context); assertThat(registry.get("http.client.requests").meters()).hasSize(2); - assertThat(this.output.toString()).contains( + assertThat(capturedOutput).contains( "Reached the maximum number of URI tags for 'http.client.requests'.") .contains("Are you using 'uriVariables'?"); }); } @Test - public void shouldNotDenyNorLogIfMaxUrisIsNotReached() { + public void shouldNotDenyNorLogIfMaxUrisIsNotReached(CapturedOutput capturedOutput) { this.contextRunner .withPropertyValues("management.metrics.web.client.max-uri-tags=5") .run((context) -> { MeterRegistry registry = getInitializedMeterRegistry(context); assertThat(registry.get("http.client.requests").meters()).hasSize(3); - assertThat(this.output.toString()).doesNotContain( + assertThat(capturedOutput).doesNotContain( "Reached the maximum number of URI tags for 'http.client.requests'.") .doesNotContain("Are you using 'uriVariables'?"); }); diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/client/WebClientMetricsConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/client/WebClientMetricsConfigurationTests.java index 074bf63412..e5ca1169ad 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/client/WebClientMetricsConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/client/WebClientMetricsConfigurationTests.java @@ -22,7 +22,7 @@ import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.Timer; import io.micrometer.core.instrument.distribution.HistogramSnapshot; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import reactor.core.publisher.Mono; import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun; @@ -31,8 +31,8 @@ import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration; import org.springframework.boot.test.context.assertj.AssertableApplicationContext; import org.springframework.boot.test.context.runner.ApplicationContextRunner; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpStatus; @@ -51,6 +51,7 @@ import static org.mockito.Mockito.mock; * @author Brian Clozel * @author Stephane Nicoll */ +@ExtendWith(OutputCaptureExtension.class) public class WebClientMetricsConfigurationTests { private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() @@ -58,9 +59,6 @@ public class WebClientMetricsConfigurationTests { .withConfiguration(AutoConfigurations.of(WebClientAutoConfiguration.class, HttpClientMetricsAutoConfiguration.class)); - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test public void webClientCreatedWithBuilderIsInstrumented() { this.contextRunner.run((context) -> { @@ -79,26 +77,26 @@ public class WebClientMetricsConfigurationTests { } @Test - public void afterMaxUrisReachedFurtherUrisAreDenied() { + public void afterMaxUrisReachedFurtherUrisAreDenied(CapturedOutput capturedOutput) { this.contextRunner .withPropertyValues("management.metrics.web.client.max-uri-tags=2") .run((context) -> { MeterRegistry registry = getInitializedMeterRegistry(context); assertThat(registry.get("http.client.requests").meters()).hasSize(2); - assertThat(this.output.toString()).contains( + assertThat(capturedOutput).contains( "Reached the maximum number of URI tags for 'http.client.requests'.") .contains("Are you using 'uriVariables'?"); }); } @Test - public void shouldNotDenyNorLogIfMaxUrisIsNotReached() { + public void shouldNotDenyNorLogIfMaxUrisIsNotReached(CapturedOutput capturedOutput) { this.contextRunner .withPropertyValues("management.metrics.web.client.max-uri-tags=5") .run((context) -> { MeterRegistry registry = getInitializedMeterRegistry(context); assertThat(registry.get("http.client.requests").meters()).hasSize(3); - assertThat(this.output.toString()).doesNotContain( + assertThat(capturedOutput).doesNotContain( "Reached the maximum number of URI tags for 'http.client.requests'.") .doesNotContain("Are you using 'uriVariables'?"); }); diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/reactive/WebFluxMetricsAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/reactive/WebFluxMetricsAutoConfigurationTests.java index cb5c87014e..49e919c574 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/reactive/WebFluxMetricsAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/reactive/WebFluxMetricsAutoConfigurationTests.java @@ -18,7 +18,7 @@ package org.springframework.boot.actuate.autoconfigure.metrics.web.reactive; import io.micrometer.core.instrument.MeterRegistry; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun; import org.springframework.boot.actuate.autoconfigure.metrics.web.TestController; @@ -29,8 +29,8 @@ import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration; import org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext; import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.test.web.reactive.server.WebTestClient; @@ -44,15 +44,13 @@ import static org.mockito.Mockito.mock; * @author Brian Clozel * @author Dmytro Nosan */ +@ExtendWith(OutputCaptureExtension.class) public class WebFluxMetricsAutoConfigurationTests { private ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner() .with(MetricsRun.simple()).withConfiguration( AutoConfigurations.of(WebFluxMetricsAutoConfiguration.class)); - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test public void shouldProvideWebFluxMetricsBeans() { this.contextRunner.run((context) -> { @@ -69,7 +67,7 @@ public class WebFluxMetricsAutoConfigurationTests { } @Test - public void afterMaxUrisReachedFurtherUrisAreDenied() { + public void afterMaxUrisReachedFurtherUrisAreDenied(CapturedOutput capturedOutput) { this.contextRunner .withConfiguration(AutoConfigurations.of(WebFluxAutoConfiguration.class)) .withUserConfiguration(TestController.class) @@ -77,14 +75,14 @@ public class WebFluxMetricsAutoConfigurationTests { .run((context) -> { MeterRegistry registry = getInitializedMeterRegistry(context); assertThat(registry.get("http.server.requests").meters()).hasSize(2); - assertThat(this.output.toString()) + assertThat(capturedOutput) .contains("Reached the maximum number of URI tags " + "for 'http.server.requests'"); }); } @Test - public void shouldNotDenyNorLogIfMaxUrisIsNotReached() { + public void shouldNotDenyNorLogIfMaxUrisIsNotReached(CapturedOutput capturedOutput) { this.contextRunner .withConfiguration(AutoConfigurations.of(WebFluxAutoConfiguration.class)) .withUserConfiguration(TestController.class) @@ -92,7 +90,7 @@ public class WebFluxMetricsAutoConfigurationTests { .run((context) -> { MeterRegistry registry = getInitializedMeterRegistry(context); assertThat(registry.get("http.server.requests").meters()).hasSize(3); - assertThat(this.output.toString()).doesNotContain( + assertThat(capturedOutput).doesNotContain( "Reached the maximum number of URI tags for 'http.server.requests'"); }); } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfigurationTests.java index 66728d6681..a72026551e 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/web/servlet/WebMvcMetricsAutoConfigurationTests.java @@ -29,7 +29,7 @@ import io.micrometer.core.instrument.Tag; import io.micrometer.core.instrument.Timer; import io.micrometer.core.instrument.distribution.HistogramSnapshot; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun; @@ -42,8 +42,8 @@ import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext; import org.springframework.boot.test.context.runner.WebApplicationContextRunner; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -63,15 +63,13 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. * @author Dmytro Nosan * @author Tadaya Tsuyukubo */ +@ExtendWith(OutputCaptureExtension.class) public class WebMvcMetricsAutoConfigurationTests { private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() .with(MetricsRun.simple()).withConfiguration( AutoConfigurations.of(WebMvcMetricsAutoConfiguration.class)); - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test public void backsOffWhenMeterRegistryIsMissing() { new WebApplicationContextRunner() @@ -112,7 +110,7 @@ public class WebMvcMetricsAutoConfigurationTests { } @Test - public void afterMaxUrisReachedFurtherUrisAreDenied() { + public void afterMaxUrisReachedFurtherUrisAreDenied(CapturedOutput capturedOutput) { this.contextRunner.withUserConfiguration(TestController.class) .withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class, WebMvcAutoConfiguration.class)) @@ -120,14 +118,14 @@ public class WebMvcMetricsAutoConfigurationTests { .run((context) -> { MeterRegistry registry = getInitializedMeterRegistry(context); assertThat(registry.get("http.server.requests").meters()).hasSize(2); - assertThat(this.output.toString()) + assertThat(capturedOutput) .contains("Reached the maximum number of URI tags " + "for 'http.server.requests'"); }); } @Test - public void shouldNotDenyNorLogIfMaxUrisIsNotReached() { + public void shouldNotDenyNorLogIfMaxUrisIsNotReached(CapturedOutput capturedOutput) { this.contextRunner.withUserConfiguration(TestController.class) .withConfiguration(AutoConfigurations.of(MetricsAutoConfiguration.class, WebMvcAutoConfiguration.class)) @@ -135,7 +133,7 @@ public class WebMvcMetricsAutoConfigurationTests { .run((context) -> { MeterRegistry registry = getInitializedMeterRegistry(context); assertThat(registry.get("http.server.requests").meters()).hasSize(3); - assertThat(this.output.toString()) + assertThat(capturedOutput) .doesNotContain("Reached the maximum number of URI tags " + "for 'http.server.requests'"); }); diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfigurationTests.java index 2cec658ad4..e648e9deab 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfigurationTests.java @@ -19,7 +19,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration; @@ -27,8 +27,8 @@ import org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagem import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; import org.springframework.boot.test.context.runner.WebApplicationContextRunner; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext; import static org.assertj.core.api.Assertions.assertThat; @@ -39,13 +39,12 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Madhura Bhave * @author Andy Wilkinson */ +@ExtendWith(OutputCaptureExtension.class) public class ManagementContextAutoConfigurationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test - public void childManagementContextShouldStartForEmbeddedServer() { + public void childManagementContextShouldStartForEmbeddedServer( + CapturedOutput capturedOutput) { WebApplicationContextRunner contextRunner = new WebApplicationContextRunner( AnnotationConfigServletWebServerApplicationContext::new) .withConfiguration(AutoConfigurations.of( @@ -54,12 +53,12 @@ public class ManagementContextAutoConfigurationTests { ServletManagementContextAutoConfiguration.class, WebEndpointAutoConfiguration.class, EndpointAutoConfiguration.class)); - contextRunner.withPropertyValues("server.port=0", "management.server.port=0").run( - (context) -> assertThat(tomcatStartedOccurencesIn(this.output.toString())) + contextRunner.withPropertyValues("server.port=0", "management.server.port=0") + .run((context) -> assertThat(tomcatStartedOccurencesIn(capturedOutput)) .isEqualTo(2)); } - private int tomcatStartedOccurencesIn(String output) { + private int tomcatStartedOccurencesIn(CharSequence output) { int matches = 0; Matcher matcher = Pattern.compile("Tomcat started on port").matcher(output); while (matcher.find()) { diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/EndpointIdTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/EndpointIdTests.java index 8f3fddf980..427ea75c74 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/EndpointIdTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/EndpointIdTests.java @@ -17,10 +17,10 @@ package org.springframework.boot.actuate.endpoint; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; @@ -30,11 +30,9 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException * * @author Phillip Webb */ +@ExtendWith(OutputCaptureExtension.class) public class EndpointIdTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test public void ofWhenNullThrowsException() { assertThatIllegalArgumentException().isThrownBy(() -> EndpointId.of(null)) @@ -88,10 +86,10 @@ public class EndpointIdTests { } @Test - public void ofWhenContainsDeprecatedCharsLogsWarning() { + public void ofWhenContainsDeprecatedCharsLogsWarning(CapturedOutput capturedOutput) { EndpointId.resetLoggedWarnings(); EndpointId.of("foo-bar"); - assertThat(this.output.toString()).contains( + assertThat(capturedOutput.toString()).contains( "Endpoint ID 'foo-bar' contains invalid characters, please migrate to a valid format"); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java index 82446b856d..221f6c79ea 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java @@ -79,6 +79,7 @@ public class MongoProperties { /** * Login user of the mongo server. Cannot be set with URI. */ + private String username; /** diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationTests.java index 60b6aa29e7..e5f8134931 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/freemarker/FreeMarkerAutoConfigurationTests.java @@ -20,12 +20,12 @@ import java.io.File; import java.io.StringWriter; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ApplicationContextRunner; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.boot.testsupport.BuildOutput; import static org.assertj.core.api.Assertions.assertThat; @@ -36,11 +36,9 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Andy Wilkinson * @author Kazuki Shimizu */ +@ExtendWith(OutputCaptureExtension.class) public class FreeMarkerAutoConfigurationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - private final BuildOutput buildOutput = new BuildOutput(getClass()); private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() @@ -62,33 +60,33 @@ public class FreeMarkerAutoConfigurationTests { } @Test - public void nonExistentTemplateLocation() { + public void nonExistentTemplateLocation(CapturedOutput capturedOutput) { this.contextRunner .withPropertyValues("spring.freemarker.templateLoaderPath:" + "classpath:/does-not-exist/,classpath:/also-does-not-exist") - .run((context) -> assertThat(this.output) + .run((context) -> assertThat(capturedOutput) .contains("Cannot find template location")); } @Test - public void emptyTemplateLocation() { + public void emptyTemplateLocation(CapturedOutput capturedOutput) { File emptyDirectory = new File(this.buildOutput.getTestResourcesLocation(), "empty-templates/empty-directory"); emptyDirectory.mkdirs(); this.contextRunner .withPropertyValues("spring.freemarker.templateLoaderPath:" + "classpath:/empty-templates/empty-directory/") - .run((context) -> assertThat(this.output.toString()) + .run((context) -> assertThat(capturedOutput) .doesNotContain("Cannot find template location")); } @Test - public void nonExistentLocationAndEmptyLocation() { + public void nonExistentLocationAndEmptyLocation(CapturedOutput capturedOutput) { new File(this.buildOutput.getTestResourcesLocation(), "empty-templates/empty-directory").mkdirs(); this.contextRunner.withPropertyValues("spring.freemarker.templateLoaderPath:" + "classpath:/does-not-exist/,classpath:/empty-templates/empty-directory/") - .run((context) -> assertThat(this.output.toString()) + .run((context) -> assertThat(capturedOutput) .doesNotContain("Cannot find template location")); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java index 57c9a60b98..c3076c850a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTests.java @@ -21,14 +21,11 @@ import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient; import org.influxdb.InfluxDB; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; import retrofit2.Retrofit; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.assertj.AssertableApplicationContext; import org.springframework.boot.test.context.runner.ApplicationContextRunner; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.test.util.ReflectionTestUtils; @@ -44,9 +41,6 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class InfluxDbAutoConfigurationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration(AutoConfigurations.of(InfluxDbAutoConfiguration.class)); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationServletContainerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationServletContainerTests.java index 4b727be724..2b13f89eda 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationServletContainerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationServletContainerTests.java @@ -36,8 +36,8 @@ import org.springframework.boot.autoconfigure.jersey.JerseyAutoConfigurationServ import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -54,7 +54,7 @@ import static org.assertj.core.api.Assertions.assertThat; */ @SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT) @DirtiesContext -@ExtendWith(OutputExtension.class) +@ExtendWith(OutputCaptureExtension.class) public class JerseyAutoConfigurationServletContainerTests { @Test diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java index c84794d436..afcc9e8f84 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfigurationTests.java @@ -43,8 +43,8 @@ import org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationList import org.springframework.boot.test.context.assertj.AssertableApplicationContext; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ContextConsumer; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.boot.testsupport.Assume; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -64,7 +64,7 @@ import static org.assertj.core.api.Assertions.contentOf; * @author Stephane Nicoll * @author Dominic Gunn */ -@ExtendWith(OutputExtension.class) +@ExtendWith(OutputCaptureExtension.class) public class LiquibaseAutoConfigurationTests { @BeforeEach diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggingListenerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggingListenerTests.java index 799b737ed5..54f427f8c0 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggingListenerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggingListenerTests.java @@ -22,7 +22,7 @@ import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.LoggerContext; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.slf4j.impl.StaticLoggerBinder; import org.springframework.boot.SpringApplication; @@ -32,8 +32,8 @@ import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConf import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; import org.springframework.boot.context.event.ApplicationFailedEvent; import org.springframework.boot.logging.LogLevel; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; @@ -53,26 +53,24 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException * @author Andy Wilkinson * @author Madhura Bhave */ +@ExtendWith(OutputCaptureExtension.class) public class ConditionEvaluationReportLoggingListenerTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - private ConditionEvaluationReportLoggingListener initializer = new ConditionEvaluationReportLoggingListener(); @Test - public void logsDebugOnContextRefresh() { + public void logsDebugOnContextRefresh(CapturedOutput capturedOutput) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); this.initializer.initialize(context); context.register(Config.class); context.refresh(); withDebugLogging(() -> this.initializer .onApplicationEvent(new ContextRefreshedEvent(context))); - assertThat(this.output).contains("CONDITIONS EVALUATION REPORT"); + assertThat(capturedOutput).contains("CONDITIONS EVALUATION REPORT"); } @Test - public void logsDebugOnError() { + public void logsDebugOnError(CapturedOutput capturedOutput) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); this.initializer.initialize(context); context.register(ErrorConfig.class); @@ -80,11 +78,11 @@ public class ConditionEvaluationReportLoggingListenerTests { (ex) -> withDebugLogging(() -> this.initializer.onApplicationEvent( new ApplicationFailedEvent(new SpringApplication(), new String[0], context, ex)))); - assertThat(this.output).contains("CONDITIONS EVALUATION REPORT"); + assertThat(capturedOutput).contains("CONDITIONS EVALUATION REPORT"); } @Test - public void logsInfoOnErrorIfDebugDisabled() { + public void logsInfoOnErrorIfDebugDisabled(CapturedOutput capturedOutput) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); this.initializer.initialize(context); context.register(ErrorConfig.class); @@ -92,13 +90,13 @@ public class ConditionEvaluationReportLoggingListenerTests { .satisfies((ex) -> this.initializer.onApplicationEvent( new ApplicationFailedEvent(new SpringApplication(), new String[0], context, ex))); - assertThat(this.output).contains("Error starting" + assertThat(capturedOutput).contains("Error starting" + " ApplicationContext. To display the conditions report re-run" + " your application with 'debug' enabled."); } @Test - public void logsOutput() { + public void logsOutput(CapturedOutput capturedOutput) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); this.initializer.initialize(context); context.register(Config.class); @@ -107,7 +105,7 @@ public class ConditionEvaluationReportLoggingListenerTests { context.refresh(); withDebugLogging(() -> this.initializer .onApplicationEvent(new ContextRefreshedEvent(context))); - assertThat(this.output) + assertThat(capturedOutput) .contains("not a servlet web application (OnWebApplicationCondition)"); } @@ -131,7 +129,7 @@ public class ConditionEvaluationReportLoggingListenerTests { } @Test - public void listenerWithInfoLevelShouldLogAtInfo() { + public void listenerWithInfoLevelShouldLogAtInfo(CapturedOutput capturedOutput) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); ConditionEvaluationReportLoggingListener initializer = new ConditionEvaluationReportLoggingListener( LogLevel.INFO); @@ -139,7 +137,7 @@ public class ConditionEvaluationReportLoggingListenerTests { context.register(Config.class); context.refresh(); initializer.onApplicationEvent(new ContextRefreshedEvent(context)); - assertThat(this.output).contains("CONDITIONS EVALUATION REPORT"); + assertThat(capturedOutput).contains("CONDITIONS EVALUATION REPORT"); } @Test @@ -150,11 +148,11 @@ public class ConditionEvaluationReportLoggingListenerTests { } @Test - public void noErrorIfNotInitialized() { + public void noErrorIfNotInitialized(CapturedOutput capturedOutput) { this.initializer .onApplicationEvent(new ApplicationFailedEvent(new SpringApplication(), new String[0], null, new RuntimeException("Planned"))); - assertThat(this.output).contains("Unable to provide the conditions report"); + assertThat(capturedOutput).contains("Unable to provide the conditions report"); } private void withDebugLogging(Runnable runnable) { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfigurationTests.java index 05d5caf636..7591a8a368 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/quartz/QuartzAutoConfigurationTests.java @@ -21,7 +21,7 @@ import java.util.concurrent.Executor; import javax.sql.DataSource; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.quartz.Calendar; import org.quartz.JobBuilder; import org.quartz.JobDetail; @@ -45,8 +45,8 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerA import org.springframework.boot.test.context.assertj.AssertableApplicationContext; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ContextConsumer; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -68,11 +68,9 @@ import static org.mockito.Mockito.verifyZeroInteractions; * @author Vedran Pavic * @author Stephane Nicoll */ +@ExtendWith(OutputCaptureExtension.class) public class QuartzAutoConfigurationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withPropertyValues("spring.datasource.generate-unique-name=true") .withConfiguration(AutoConfigurations.of(QuartzAutoConfiguration.class)); @@ -176,7 +174,7 @@ public class QuartzAutoConfigurationTests { } @Test - public void withConfiguredJobAndTrigger() { + public void withConfiguredJobAndTrigger(CapturedOutput capturedOutput) { this.contextRunner.withUserConfiguration(QuartzFullConfiguration.class) .withPropertyValues("test-name=withConfiguredJobAndTrigger") .run((context) -> { @@ -187,8 +185,8 @@ public class QuartzAutoConfigurationTests { assertThat(scheduler.getTrigger(TriggerKey.triggerKey("fooTrigger"))) .isNotNull(); Thread.sleep(1000L); - assertThat(this.output).contains("withConfiguredJobAndTrigger"); - assertThat(this.output).contains("jobDataValue"); + assertThat(capturedOutput).contains("withConfiguredJobAndTrigger") + .contains("jobDataValue"); }); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityAutoConfigurationTests.java index 20b91ed018..5652e4cce9 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityAutoConfigurationTests.java @@ -21,7 +21,6 @@ import java.util.EnumSet; import javax.servlet.DispatcherType; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.TestAutoConfigurationPackage; @@ -30,8 +29,6 @@ import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.test.City; import org.springframework.boot.test.context.runner.WebApplicationContextRunner; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; import org.springframework.boot.web.servlet.DelegatingFilterProxyRegistrationBean; import org.springframework.boot.web.servlet.filter.OrderedFilter; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -65,9 +62,6 @@ public class SecurityAutoConfigurationTests { .withConfiguration(AutoConfigurations.of(SecurityAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class)); - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test public void testWebConfiguration() { this.contextRunner.run((context) -> { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityFilterAutoConfigurationEarlyInitializationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityFilterAutoConfigurationEarlyInitializationTests.java index ce0eb23408..fc1d936367 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityFilterAutoConfigurationEarlyInitializationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityFilterAutoConfigurationEarlyInitializationTests.java @@ -21,7 +21,7 @@ import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.deser.std.StdDeserializer; import com.fasterxml.jackson.databind.module.SimpleModule; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; @@ -30,8 +30,8 @@ import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConf import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration; import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; @@ -51,19 +51,18 @@ import org.springframework.web.bind.annotation.RestController; * * @author Phillip Webb */ +@ExtendWith(OutputCaptureExtension.class) public class SecurityFilterAutoConfigurationEarlyInitializationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test - public void testSecurityFilterDoesNotCauseEarlyInitialization() { + public void testSecurityFilterDoesNotCauseEarlyInitialization( + CapturedOutput capturedOutput) { try (AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext()) { TestPropertyValues.of("server.port:0").applyTo(context); context.register(Config.class); context.refresh(); int port = context.getWebServer().getPort(); - String password = this.output.toString() + String password = capturedOutput.toString() .split("Using generated security password: ")[1].split("\n")[0] .trim(); new TestRestTemplate("user", password) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/UserDetailsServiceAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/UserDetailsServiceAutoConfigurationTests.java index 2983740076..8b00a05935 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/UserDetailsServiceAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/UserDetailsServiceAutoConfigurationTests.java @@ -19,14 +19,14 @@ package org.springframework.boot.autoconfigure.security.servlet; import java.util.Collections; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.security.SecurityProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.test.context.runner.ApplicationContextRunner; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -52,27 +52,26 @@ import static org.mockito.Mockito.mock; * * @author Madhura Bhave */ +@ExtendWith(OutputCaptureExtension.class) public class UserDetailsServiceAutoConfigurationTests { private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withUserConfiguration(TestSecurityConfiguration.class).withConfiguration( AutoConfigurations.of(UserDetailsServiceAutoConfiguration.class)); - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test - public void testDefaultUsernamePassword() { + public void testDefaultUsernamePassword(CapturedOutput capturedOutput) { this.contextRunner.run((context) -> { UserDetailsService manager = context.getBean(UserDetailsService.class); - assertThat(this.output.toString()) + assertThat(capturedOutput.toString()) .contains("Using generated security password:"); assertThat(manager.loadUserByUsername("user")).isNotNull(); }); } @Test - public void defaultUserNotCreatedIfAuthenticationManagerBeanPresent() { + public void defaultUserNotCreatedIfAuthenticationManagerBeanPresent( + CapturedOutput capturedOutput) { this.contextRunner .withUserConfiguration(TestAuthenticationManagerConfiguration.class) .run((context) -> { @@ -80,7 +79,7 @@ public class UserDetailsServiceAutoConfigurationTests { .getBean(AuthenticationManager.class); assertThat(manager).isEqualTo(context.getBean( TestAuthenticationManagerConfiguration.class).authenticationManager); - assertThat(this.output.toString()) + assertThat(capturedOutput.toString()) .doesNotContain("Using generated security password: "); TestingAuthenticationToken token = new TestingAuthenticationToken( "foo", "bar"); @@ -89,26 +88,28 @@ public class UserDetailsServiceAutoConfigurationTests { } @Test - public void defaultUserNotCreatedIfUserDetailsServiceBeanPresent() { + public void defaultUserNotCreatedIfUserDetailsServiceBeanPresent( + CapturedOutput capturedOutput) { this.contextRunner .withUserConfiguration(TestUserDetailsServiceConfiguration.class) .run((context) -> { UserDetailsService userDetailsService = context .getBean(UserDetailsService.class); - assertThat(this.output.toString()) + assertThat(capturedOutput.toString()) .doesNotContain("Using generated security password: "); assertThat(userDetailsService.loadUserByUsername("foo")).isNotNull(); }); } @Test - public void defaultUserNotCreatedIfAuthenticationProviderBeanPresent() { + public void defaultUserNotCreatedIfAuthenticationProviderBeanPresent( + CapturedOutput capturedOutput) { this.contextRunner .withUserConfiguration(TestAuthenticationProviderConfiguration.class) .run((context) -> { AuthenticationProvider provider = context .getBean(AuthenticationProvider.class); - assertThat(this.output.toString()) + assertThat(capturedOutput.toString()) .doesNotContain("Using generated security password: "); TestingAuthenticationToken token = new TestingAuthenticationToken( "foo", "bar"); @@ -153,10 +154,11 @@ public class UserDetailsServiceAutoConfigurationTests { } @Test - public void generatedPasswordShouldNotBePrintedIfAuthenticationManagerBuilderIsUsed() { + public void generatedPasswordShouldNotBePrintedIfAuthenticationManagerBuilderIsUsed( + CapturedOutput capturedOutput) { this.contextRunner .withUserConfiguration(TestConfigWithAuthenticationManagerBuilder.class) - .run(((context) -> assertThat(this.output.toString()) + .run(((context) -> assertThat(capturedOutput.toString()) .doesNotContain("Using generated security password: "))); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskExecutionAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskExecutionAutoConfigurationTests.java index 7330fddeb0..d91f6801d0 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskExecutionAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/task/TaskExecutionAutoConfigurationTests.java @@ -21,7 +21,7 @@ import java.util.concurrent.Future; import java.util.function.Consumer; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.task.TaskExecutorBuilder; @@ -29,8 +29,8 @@ import org.springframework.boot.task.TaskExecutorCustomizer; import org.springframework.boot.test.context.assertj.AssertableApplicationContext; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.ContextConsumer; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.task.SyncTaskExecutor; @@ -53,15 +53,13 @@ import static org.mockito.Mockito.verify; * @author Stephane Nicoll * @author Camille Vienot */ +@ExtendWith(OutputCaptureExtension.class) public class TaskExecutionAutoConfigurationTests { private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration( AutoConfigurations.of(TaskExecutionAutoConfiguration.class)); - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test public void taskExecutorBuilderShouldApplyCustomSettings() { this.contextRunner @@ -113,15 +111,14 @@ public class TaskExecutionAutoConfigurationTests { } @Test - public void taskExecutorAutoConfigured() { + public void taskExecutorAutoConfigured(CapturedOutput capturedOutput) { this.contextRunner.run((context) -> { - assertThat(this.output.toString()) - .doesNotContain("Initializing ExecutorService"); + assertThat(capturedOutput).doesNotContain("Initializing ExecutorService"); assertThat(context).hasSingleBean(Executor.class); assertThat(context).hasBean("applicationTaskExecutor"); assertThat(context).getBean("applicationTaskExecutor") .isInstanceOf(ThreadPoolTaskExecutor.class); - assertThat(this.output.toString()).contains("Initializing ExecutorService"); + assertThat(capturedOutput).contains("Initializing ExecutorService"); }); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafReactiveAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafReactiveAutoConfigurationTests.java index 8421a5162d..c25106d5bd 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafReactiveAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafReactiveAutoConfigurationTests.java @@ -23,7 +23,7 @@ import java.util.Locale; import nz.net.ultraq.thymeleaf.LayoutDialect; import nz.net.ultraq.thymeleaf.decorators.strategies.GroupingRespectLayoutTitleStrategy; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context; import org.thymeleaf.context.IContext; @@ -37,8 +37,8 @@ import org.thymeleaf.templateresolver.ITemplateResolver; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.boot.testsupport.BuildOutput; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -58,11 +58,9 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Kazuki Shimizu * @author Stephane Nicoll */ +@ExtendWith(OutputCaptureExtension.class) public class ThymeleafReactiveAutoConfigurationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - private final BuildOutput buildOutput = new BuildOutput(getClass()); private ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner() @@ -187,21 +185,21 @@ public class ThymeleafReactiveAutoConfigurationTests { } @Test - public void templateLocationDoesNotExist() { + public void templateLocationDoesNotExist(CapturedOutput capturedOutput) { this.contextRunner .withPropertyValues( "spring.thymeleaf.prefix:classpath:/no-such-directory/") - .run((context) -> assertThat(this.output) + .run((context) -> assertThat(capturedOutput) .contains("Cannot find template location")); } @Test - public void templateLocationEmpty() { + public void templateLocationEmpty(CapturedOutput capturedOutput) { new File(this.buildOutput.getTestResourcesLocation(), "empty-templates/empty-directory").mkdirs(); this.contextRunner.withPropertyValues( "spring.thymeleaf.prefix:classpath:/empty-templates/empty-directory/") - .run((context) -> assertThat(this.output.toString()) + .run((context) -> assertThat(capturedOutput) .doesNotContain("Cannot find template location")); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java index eee820a7e8..a0b847bf45 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafServletAutoConfigurationTests.java @@ -41,7 +41,7 @@ import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.FilteredClassLoader; import org.springframework.boot.test.context.runner.ApplicationContextRunner; import org.springframework.boot.test.context.runner.WebApplicationContextRunner; -import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.system.OutputCaptureRule; import org.springframework.boot.testsupport.BuildOutput; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.filter.OrderedCharacterEncodingFilter; @@ -75,7 +75,7 @@ import static org.hamcrest.Matchers.not; public class ThymeleafServletAutoConfigurationTests { @Rule - public OutputCapture output = new OutputCapture(); + public OutputCaptureRule output = new OutputCaptureRule(); private final BuildOutput buildOutput = new BuildOutput(getClass()); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandlerIntegrationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandlerIntegrationTests.java index 8c67f04440..e8c8681d39 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandlerIntegrationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/error/DefaultErrorWebExceptionHandlerIntegrationTests.java @@ -21,7 +21,7 @@ import java.nio.charset.StandardCharsets; import javax.validation.Valid; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import reactor.core.publisher.Mono; import org.springframework.boot.autoconfigure.AutoConfigurations; @@ -32,8 +32,8 @@ import org.springframework.boot.autoconfigure.web.reactive.ReactiveWebServerFact import org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration; import org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext; import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -56,6 +56,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; * * @author Brian Clozel */ +@ExtendWith(OutputCaptureExtension.class) public class DefaultErrorWebExceptionHandlerIntegrationTests { private static final MediaType TEXT_HTML_UTF8 = new MediaType("text", "html", @@ -63,9 +64,6 @@ public class DefaultErrorWebExceptionHandlerIntegrationTests { private final LogIdFilter logIdFilter = new LogIdFilter(); - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - private ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner() .withConfiguration(AutoConfigurations.of( ReactiveWebServerFactoryAutoConfiguration.class, @@ -78,7 +76,7 @@ public class DefaultErrorWebExceptionHandlerIntegrationTests { .withUserConfiguration(Application.class); @Test - public void jsonError() { + public void jsonError(CapturedOutput capturedOutput) { this.contextRunner.run((context) -> { WebTestClient client = getWebClient(context); client.get().uri("/").exchange().expectStatus() @@ -89,7 +87,7 @@ public class DefaultErrorWebExceptionHandlerIntegrationTests { .isEqualTo("Expected!").jsonPath("exception").doesNotExist() .jsonPath("trace").doesNotExist().jsonPath("requestId") .isEqualTo(this.logIdFilter.getLogId()); - assertThat(this.output).contains("500 Server Error for HTTP GET \"/\"") + assertThat(capturedOutput).contains("500 Server Error for HTTP GET \"/\"") .contains("java.lang.IllegalStateException: Expected!"); }); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfigurationTests.java index 6788b7a0de..57717b3105 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfigurationTests.java @@ -17,13 +17,13 @@ package org.springframework.boot.autoconfigure.web.servlet.error; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration; import org.springframework.boot.test.context.runner.WebApplicationContextRunner; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.boot.web.servlet.error.ErrorAttributes; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; @@ -38,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Brian Clozel */ +@ExtendWith(OutputCaptureExtension.class) public class ErrorMvcAutoConfigurationTests { private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() @@ -45,9 +46,6 @@ public class ErrorMvcAutoConfigurationTests { AutoConfigurations.of(DispatcherServletAutoConfiguration.class, ErrorMvcAutoConfiguration.class)); - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test public void renderContainsViewWithExceptionDetails() throws Exception { this.contextRunner.run((context) -> { @@ -69,7 +67,7 @@ public class ErrorMvcAutoConfigurationTests { } @Test - public void renderWhenAlreadyCommittedLogsMessage() { + public void renderWhenAlreadyCommittedLogsMessage(CapturedOutput capturedOutput) { this.contextRunner.run((context) -> { View errorView = context.getBean("error", View.class); ErrorAttributes errorAttributes = context.getBean(ErrorAttributes.class); @@ -77,7 +75,7 @@ public class ErrorMvcAutoConfigurationTests { new IllegalStateException("Exception message"), true); errorView.render(errorAttributes.getErrorAttributes(webRequest, true), webRequest.getRequest(), webRequest.getResponse()); - assertThat(this.output) + assertThat(capturedOutput) .contains("Cannot render error page for request [/path] " + "and exception [Exception message] as the response has " + "already been committed. As a result, the response may " diff --git a/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java b/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java index 57fd3b9384..9a84f5a466 100644 --- a/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java +++ b/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/CliTester.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -42,7 +42,7 @@ import org.springframework.boot.cli.command.OptionParsingCommand; import org.springframework.boot.cli.command.archive.JarCommand; import org.springframework.boot.cli.command.grab.GrabCommand; import org.springframework.boot.cli.command.run.RunCommand; -import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.system.OutputCaptureRule; import org.springframework.boot.testsupport.BuildOutput; import org.springframework.util.FileCopyUtils; import org.springframework.util.StringUtils; @@ -60,7 +60,9 @@ public class CliTester implements TestRule { private final BuildOutput buildOutput = new BuildOutput(getClass()); - private final OutputCapture outputCapture = new OutputCapture(); + private final OutputCaptureRule outputCapture = new OutputCaptureRule(); + + private String previousOutput = ""; private long timeout = TimeUnit.MINUTES.toMillis(6); @@ -170,8 +172,9 @@ public class CliTester implements TestRule { } private String getOutput() { - String output = this.outputCapture.toString(); - this.outputCapture.reset(); + String output = this.outputCapture.toString() + .substring(this.previousOutput.length()); + this.previousOutput = output; return output; } diff --git a/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/CommandRunnerIntegrationTests.java b/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/CommandRunnerIntegrationTests.java index 27fbe04212..ec03be289e 100644 --- a/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/CommandRunnerIntegrationTests.java +++ b/spring-boot-project/spring-boot-cli/src/test/java/org/springframework/boot/cli/command/CommandRunnerIntegrationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,7 @@ import org.junit.Rule; import org.junit.Test; import org.springframework.boot.cli.command.run.RunCommand; -import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.system.OutputCaptureRule; import static org.assertj.core.api.Assertions.assertThat; @@ -30,7 +30,7 @@ import static org.assertj.core.api.Assertions.assertThat; public class CommandRunnerIntegrationTests { @Rule - public OutputCapture output = new OutputCapture(); + public OutputCaptureRule output = new OutputCaptureRule(); @Test public void debugAddsAutoconfigReport() { diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/remote/client/RemoteClientConfigurationTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/remote/client/RemoteClientConfigurationTests.java index f17141b7ef..cefe02f457 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/remote/client/RemoteClientConfigurationTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/remote/client/RemoteClientConfigurationTests.java @@ -37,7 +37,7 @@ import org.springframework.boot.devtools.remote.server.Dispatcher; import org.springframework.boot.devtools.remote.server.DispatcherFilter; import org.springframework.boot.devtools.restart.MockRestarter; import org.springframework.boot.devtools.restart.RestartScopeInitializer; -import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.system.OutputCaptureRule; import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory; import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext; @@ -65,7 +65,7 @@ public class RemoteClientConfigurationTests { public MockRestarter restarter = new MockRestarter(); @Rule - public OutputCapture output = new OutputCapture(); + public OutputCaptureRule output = new OutputCaptureRule(); private AnnotationConfigServletWebServerApplicationContext context; diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestartApplicationListenerTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestartApplicationListenerTests.java index 7b89e64046..3a75c3352b 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestartApplicationListenerTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestartApplicationListenerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,7 +28,7 @@ import org.springframework.boot.context.event.ApplicationFailedEvent; import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.boot.context.event.ApplicationStartingEvent; -import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.system.OutputCaptureRule; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.Ordered; import org.springframework.test.util.ReflectionTestUtils; @@ -50,7 +50,7 @@ public class RestartApplicationListenerTests { private static final String[] ARGS = new String[] { "a", "b", "c" }; @Rule - public final OutputCapture output = new OutputCapture(); + public final OutputCaptureRule output = new OutputCaptureRule(); @Before @After @@ -86,7 +86,6 @@ public class RestartApplicationListenerTests { @Test public void disableWithSystemProperty() { System.setProperty(ENABLED_PROPERTY, "false"); - this.output.reset(); testInitialize(false); assertThat(Restarter.getInstance()).hasFieldOrPropertyWithValue("enabled", false); assertThat(this.output.toString()) diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestarterTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestarterTests.java index 459ce66850..c1dfa7bcbf 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestarterTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/restart/RestarterTests.java @@ -31,7 +31,7 @@ import org.springframework.beans.factory.ObjectFactory; import org.springframework.boot.devtools.restart.classloader.ClassLoaderFile; import org.springframework.boot.devtools.restart.classloader.ClassLoaderFile.Kind; import org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles; -import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.system.OutputCaptureRule; import org.springframework.context.ApplicationListener; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.event.ContextClosedEvent; @@ -57,7 +57,7 @@ import static org.mockito.Mockito.verifyZeroInteractions; public class RestarterTests { @Rule - public OutputCapture out = new OutputCapture(); + public OutputCaptureRule out = new OutputCaptureRule(); @Before public void setup() { diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/tunnel/client/HttpTunnelConnectionTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/tunnel/client/HttpTunnelConnectionTests.java index 89f9e943e8..9b3b460451 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/tunnel/client/HttpTunnelConnectionTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/tunnel/client/HttpTunnelConnectionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,7 +33,7 @@ import org.mockito.MockitoAnnotations; import org.springframework.boot.devtools.test.MockClientHttpRequestFactory; import org.springframework.boot.devtools.tunnel.client.HttpTunnelConnection.TunnelChannel; -import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.system.OutputCaptureRule; import org.springframework.http.HttpStatus; import static org.assertj.core.api.Assertions.assertThat; @@ -53,7 +53,7 @@ import static org.mockito.Mockito.verify; public class HttpTunnelConnectionTests { @Rule - public OutputCapture outputCapture = new OutputCapture(); + public OutputCaptureRule outputCapture = new OutputCaptureRule(); private String url; diff --git a/spring-boot-project/spring-boot-docs/src/test/java/org/springframework/boot/docs/builder/SpringApplicationBuilderExampleTests.java b/spring-boot-project/spring-boot-docs/src/test/java/org/springframework/boot/docs/builder/SpringApplicationBuilderExampleTests.java index dac738189a..0002a7ad74 100644 --- a/spring-boot-project/spring-boot-docs/src/test/java/org/springframework/boot/docs/builder/SpringApplicationBuilderExampleTests.java +++ b/spring-boot-project/spring-boot-docs/src/test/java/org/springframework/boot/docs/builder/SpringApplicationBuilderExampleTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.system.OutputCaptureRule; import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; @@ -36,7 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat; public class SpringApplicationBuilderExampleTests { @Rule - public OutputCapture outputCapture = new OutputCapture(); + public OutputCaptureRule outputCapture = new OutputCaptureRule(); @Test public void contextHierarchyWithDisabledBanner() { diff --git a/spring-boot-project/spring-boot-properties-migrator/src/test/java/org/springframework/boot/context/properties/migrator/PropertiesMigrationListenerTests.java b/spring-boot-project/spring-boot-properties-migrator/src/test/java/org/springframework/boot/context/properties/migrator/PropertiesMigrationListenerTests.java index 885acc3efd..944378487e 100644 --- a/spring-boot-project/spring-boot-properties-migrator/src/test/java/org/springframework/boot/context/properties/migrator/PropertiesMigrationListenerTests.java +++ b/spring-boot-project/spring-boot-properties-migrator/src/test/java/org/springframework/boot/context/properties/migrator/PropertiesMigrationListenerTests.java @@ -21,7 +21,7 @@ import org.junit.Rule; import org.junit.Test; import org.springframework.boot.SpringApplication; -import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.system.OutputCaptureRule; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Configuration; @@ -35,7 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat; public class PropertiesMigrationListenerTests { @Rule - public final OutputCapture output = new OutputCapture(); + public final OutputCaptureRule output = new OutputCaptureRule(); private ConfigurableApplicationContext context; diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListenerTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListenerTests.java index 2c34e91bac..470dc7bee9 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListenerTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/SpringBootDependencyInjectionTestExecutionListenerTests.java @@ -23,7 +23,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.WebApplicationType; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; -import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.system.OutputCaptureRule; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; @@ -44,7 +44,7 @@ import static org.mockito.Mockito.mock; public class SpringBootDependencyInjectionTestExecutionListenerTests { @Rule - public OutputCapture out = new OutputCapture(); + public OutputCaptureRule out = new OutputCaptureRule(); private SpringBootDependencyInjectionTestExecutionListener reportListener = new SpringBootDependencyInjectionTestExecutionListener(); diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/MockMvcSpringBootTestIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/MockMvcSpringBootTestIntegrationTests.java index e4cedbb6de..d6525588e1 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/MockMvcSpringBootTestIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/MockMvcSpringBootTestIntegrationTests.java @@ -25,7 +25,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock import org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrint; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.system.OutputCaptureRule; import org.springframework.context.ApplicationContext; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.junit4.SpringRunner; @@ -49,7 +49,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. public class MockMvcSpringBootTestIntegrationTests { @Rule - public OutputCapture output = new OutputCapture(); + public OutputCaptureRule output = new OutputCaptureRule(); @MockBean private ExampleMockableService service; diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestPrintAlwaysIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestPrintAlwaysIntegrationTests.java index 9cd0208450..cc13f525a7 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestPrintAlwaysIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestPrintAlwaysIntegrationTests.java @@ -23,7 +23,7 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.system.OutputCaptureRule; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; @@ -45,7 +45,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. public class WebMvcTestPrintAlwaysIntegrationTests { @Rule - public OutputCapture output = new OutputCapture(); + public OutputCaptureRule output = new OutputCaptureRule(); @Autowired private MockMvc mvc; diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestPrintDefaultOverrideIntegrationTests.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestPrintDefaultOverrideIntegrationTests.java index c84d0fdfc9..326621ae36 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestPrintDefaultOverrideIntegrationTests.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestPrintDefaultOverrideIntegrationTests.java @@ -22,7 +22,7 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.system.OutputCaptureRule; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; @@ -45,7 +45,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. public class WebMvcTestPrintDefaultOverrideIntegrationTests { @Rule - public OutputCapture output = new OutputCapture(); + public OutputCaptureRule output = new OutputCaptureRule(); @Autowired private MockMvc mvc; diff --git a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestPrintDefaultRunner.java b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestPrintDefaultRunner.java index e9e7485503..62e4aeac7c 100644 --- a/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestPrintDefaultRunner.java +++ b/spring-boot-project/spring-boot-test-autoconfigure/src/test/java/org/springframework/boot/test/autoconfigure/web/servlet/mockmvc/WebMvcTestPrintDefaultRunner.java @@ -20,7 +20,7 @@ import org.junit.runners.model.FrameworkMethod; import org.junit.runners.model.InitializationError; import org.junit.runners.model.Statement; -import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.system.OutputCaptureRule; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.hamcrest.Matchers.containsString; @@ -41,7 +41,7 @@ public class WebMvcTestPrintDefaultRunner extends SpringJUnit4ClassRunner { protected Statement methodBlock(FrameworkMethod frameworkMethod) { Statement statement = super.methodBlock(frameworkMethod); statement = new AlwaysPassStatement(statement); - OutputCapture outputCapture = new OutputCapture(); + OutputCaptureRule outputCapture = new OutputCaptureRule(); if (frameworkMethod.getName().equals("shouldPrint")) { outputCapture.expect(containsString("HTTP Method")); } diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/rule/OutputCapture.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/rule/OutputCapture.java index 007f2b7455..1d4b1f31ed 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/rule/OutputCapture.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/rule/OutputCapture.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,23 +16,12 @@ package org.springframework.boot.test.rule; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.io.PrintStream; -import java.util.ArrayList; -import java.util.List; - import org.hamcrest.Matcher; -import org.junit.Assert; import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; -import org.springframework.boot.ansi.AnsiOutput; -import org.springframework.boot.ansi.AnsiOutput.Enabled; - -import static org.hamcrest.Matchers.allOf; +import org.springframework.boot.test.system.OutputCaptureRule; /** * JUnit {@code @Rule} to capture output from System.out and System.err. @@ -40,78 +29,32 @@ import static org.hamcrest.Matchers.allOf; * @author Phillip Webb * @author Andy Wilkinson * @since 1.4.0 + * @deprecated since 2.2.0 in favor of {@link OutputCaptureRule} */ +@Deprecated public class OutputCapture implements TestRule { - private CaptureOutputStream captureOut; - - private CaptureOutputStream captureErr; - - private ByteArrayOutputStream copy; - - private List> matchers = new ArrayList<>(); + private final OutputCaptureRule delegate = new OutputCaptureRule(); @Override public Statement apply(Statement base, Description description) { - return new Statement() { - @Override - public void evaluate() throws Throwable { - captureOutput(); - try { - base.evaluate(); - } - finally { - try { - if (!OutputCapture.this.matchers.isEmpty()) { - String output = OutputCapture.this.toString(); - Assert.assertThat(output, allOf(OutputCapture.this.matchers)); - } - } - finally { - releaseOutput(); - } - } - } - }; - } - - protected void captureOutput() { - AnsiOutputControl.get().disableAnsiOutput(); - this.copy = new ByteArrayOutputStream(); - this.captureOut = new CaptureOutputStream(System.out, this.copy); - this.captureErr = new CaptureOutputStream(System.err, this.copy); - System.setOut(new PrintStream(this.captureOut)); - System.setErr(new PrintStream(this.captureErr)); - } - - protected void releaseOutput() { - AnsiOutputControl.get().enabledAnsiOutput(); - System.setOut(this.captureOut.getOriginal()); - System.setErr(this.captureErr.getOriginal()); - this.copy = null; + return this.delegate.apply(base, description); } /** * Discard all currently accumulated output. */ public void reset() { - this.copy.reset(); + this.delegate.reset(); } public void flush() { - try { - this.captureOut.flush(); - this.captureErr.flush(); - } - catch (IOException ex) { - // ignore - } + // Flushing is no longer necessary } @Override public String toString() { - flush(); - return this.copy.toString(); + return this.delegate.toString(); } /** @@ -120,85 +63,7 @@ public class OutputCapture implements TestRule { * @param matcher the matcher */ public void expect(Matcher matcher) { - this.matchers.add(matcher); - } - - private static class CaptureOutputStream extends OutputStream { - - private final PrintStream original; - - private final OutputStream copy; - - CaptureOutputStream(PrintStream original, OutputStream copy) { - this.original = original; - this.copy = copy; - } - - @Override - public void write(int b) throws IOException { - this.copy.write(b); - this.original.write(b); - this.original.flush(); - } - - @Override - public void write(byte[] b) throws IOException { - write(b, 0, b.length); - } - - @Override - public void write(byte[] b, int off, int len) throws IOException { - this.copy.write(b, off, len); - this.original.write(b, off, len); - } - - public PrintStream getOriginal() { - return this.original; - } - - @Override - public void flush() throws IOException { - this.copy.flush(); - this.original.flush(); - } - - } - - /** - * Allow AnsiOutput to not be on the test classpath. - */ - private static class AnsiOutputControl { - - public void disableAnsiOutput() { - } - - public void enabledAnsiOutput() { - } - - public static AnsiOutputControl get() { - try { - Class.forName("org.springframework.boot.ansi.AnsiOutput"); - return new AnsiPresentOutputControl(); - } - catch (ClassNotFoundException ex) { - return new AnsiOutputControl(); - } - } - - } - - private static class AnsiPresentOutputControl extends AnsiOutputControl { - - @Override - public void disableAnsiOutput() { - AnsiOutput.setEnabled(Enabled.NEVER); - } - - @Override - public void enabledAnsiOutput() { - AnsiOutput.setEnabled(Enabled.DETECT); - } - + this.delegate.expect(matcher); } } diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/system/CapturedOutput.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/system/CapturedOutput.java new file mode 100644 index 0000000000..440ab76823 --- /dev/null +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/system/CapturedOutput.java @@ -0,0 +1,71 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.system; + +/** + * Provides access to {@link System#out System.out} and {@link System#err System.err} + * output that has been capture by the {@link OutputCaptureExtension}. Can be used to + * apply assertions either using AssertJ or standard JUnit assertions. For example: + *
+ * assertThat(output).contains("started"); // Checks all output
+ * assertThat(output.getErr()).contains("failed"); // Only checks System.err
+ * assertThat(output.getOut()).contains("ok"); // Only checks System.put
+ * 
+ * + * @author Madhura Bhave + * @author Phillip Webb + * @author Andy Wilkinson + * @since 2.2.0 + * @see OutputCaptureExtension + */ +public interface CapturedOutput extends CharSequence { + + @Override + default int length() { + return toString().length(); + } + + @Override + default char charAt(int index) { + return toString().charAt(index); + } + + @Override + default CharSequence subSequence(int start, int end) { + return toString().subSequence(start, end); + } + + /** + * Return all content (both {@link System#out System.out} and {@link System#err + * System.err}) in the order that it was was captured. + * @return all captured output + */ + String getAll(); + + /** + * Return {@link System#out System.out} content in the order that it was was captured. + * @return {@link System#out System.out} captured output + */ + String getOut(); + + /** + * Return {@link System#err System.err} content in the order that it was was captured. + * @return {@link System#err System.err} captured output + */ + String getErr(); + +} diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/extension/CapturedOutput.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/system/OutputCapture.java similarity index 86% rename from spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/extension/CapturedOutput.java rename to spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/system/OutputCapture.java index e7647c4d59..9c58eca99f 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/extension/CapturedOutput.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/system/OutputCapture.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.test.extension; +package org.springframework.boot.test.system; import java.io.IOException; import java.io.OutputStream; @@ -27,41 +27,32 @@ import java.util.List; import java.util.function.Consumer; import java.util.function.Predicate; -import org.junit.jupiter.api.extension.Extension; - import org.springframework.boot.ansi.AnsiOutput; import org.springframework.boot.ansi.AnsiOutput.Enabled; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; /** - * Provides access to {@link System#out System.out} and {@link System#err System.err} - * output that has been capture by the {@link OutputExtension}. Can be used to apply - * assertions either using AssertJ or standard JUnit assertions. For example: - *
- * assertThat(output).contains("started"); // Checks all output
- * assertThat(output.getErr()).contains("failed"); // Only checks System.err
- * assertThat(output.getOut()).contains("ok"); // Only checks System.put
- * 
+ * Provides support for capturing {@link System#out System.out} and {@link System#err + * System.err}. * * @author Madhura Bhave * @author Phillip Webb + * @author Andy Wilkinson * @since 2.2.0 - * @see OutputExtension + * @see OutputCaptureExtension + * @see OutputCaptureRule */ -public class CapturedOutput implements CharSequence, Extension { +class OutputCapture implements CapturedOutput { private final Deque systemCaptures = new ArrayDeque<>(); private AnsiOutputState ansiOutputState; - protected CapturedOutput() { - } - /** * Push a new system capture session onto the stack. */ - protected final void push() { + final void push() { if (this.systemCaptures.isEmpty()) { this.ansiOutputState = AnsiOutputState.saveAndDisable(); } @@ -71,7 +62,7 @@ public class CapturedOutput implements CharSequence, Extension { /** * Pop the last system capture session from the stack. */ - protected final void pop() { + final void pop() { this.systemCaptures.removeLast().release(); if (this.systemCaptures.isEmpty() && this.ansiOutputState != null) { this.ansiOutputState.restore(); @@ -79,21 +70,6 @@ public class CapturedOutput implements CharSequence, Extension { } } - @Override - public int length() { - return toString().length(); - } - - @Override - public char charAt(int index) { - return toString().charAt(index); - } - - @Override - public CharSequence subSequence(int start, int end) { - return toString().subSequence(start, end); - } - @Override public boolean equals(Object obj) { if (obj == this) { @@ -120,6 +96,7 @@ public class CapturedOutput implements CharSequence, Extension { * System.err}) in the order that it was was captured. * @return all captured output */ + @Override public String getAll() { return get((type) -> true); } @@ -128,6 +105,7 @@ public class CapturedOutput implements CharSequence, Extension { * Return {@link System#out System.out} content in the order that it was was captured. * @return {@link System#out System.out} captured output */ + @Override public String getOut() { return get(Type.OUT::equals); } @@ -136,10 +114,18 @@ public class CapturedOutput implements CharSequence, Extension { * Return {@link System#err System.err} content in the order that it was was captured. * @return {@link System#err System.err} captured output */ + @Override public String getErr() { return get(Type.ERR::equals); } + /** + * Resets the current capture session, clearing its captured output. + */ + void reset() { + this.systemCaptures.peek().reset(); + } + private String get(Predicate filter) { Assert.state(!this.systemCaptures.isEmpty(), "No system captures found. Check that you have used @RegisterExtension " @@ -192,6 +178,10 @@ public class CapturedOutput implements CharSequence, Extension { } } + public void reset() { + this.capturedStrings.clear(); + } + } /** @@ -303,7 +293,7 @@ public class CapturedOutput implements CharSequence, Extension { public static AnsiOutputState saveAndDisable() { if (!ClassUtils.isPresent("org.springframework.boot.ansi.AnsiOutput", - CapturedOutput.class.getClassLoader())) { + OutputCapture.class.getClassLoader())) { return null; } return new AnsiOutputState(); diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/extension/OutputExtension.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/system/OutputCaptureExtension.java similarity index 61% rename from spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/extension/OutputExtension.java rename to spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/system/OutputCaptureExtension.java index 2fbe858182..af5754f208 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/extension/OutputExtension.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/system/OutputCaptureExtension.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.test.extension; +package org.springframework.boot.test.system; import org.junit.jupiter.api.extension.AfterAllCallback; import org.junit.jupiter.api.extension.AfterEachCallback; @@ -25,74 +25,62 @@ import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.ParameterContext; import org.junit.jupiter.api.extension.ParameterResolutionException; import org.junit.jupiter.api.extension.ParameterResolver; -import org.junit.jupiter.api.extension.RegisterExtension; /** - * JUnit5 {@code @Extension} to capture output {@link System#out System.out} and + * JUnit 5 {@code @Extension} to capture {@link System#out System.out} and * {@link System#err System.err}. Can be used on a test class via - * {@link ExtendWith @ExtendWith}, or on field using - * {@link RegisterExtension @RegisterExtension}. This extension provides access to - * {@link CapturedOutput} instances which can be used to assert that the correct output - * was written. + * {@link ExtendWith @ExtendWith}. This extension provides {@link ParameterResolver + * parameter resolution} for a {@link CapturedOutput} instance which can be used to assert + * that the correct output was written. *

- * To use with {@link ExtendWith @ExtendWith}, inject the {@link CapturedOutput} as a test - * argument:

+ * To use with {@link ExtendWith @ExtendWith}, inject the {@link CapturedOutput} as an
+ * argument to your test class constructor or test method:
+ *
+ * 
  * @ExtendWith(OutputExtension.class)
  * class MyTest {
  *
- *   @Test
- *   void test(CapturedOutput output) {
- *       assertThat(output).contains("ok");
- *   }
- *
- * }
- * 
- *

- * To use with {@link RegisterExtension @RegisterExtension}, use the {@link #capture() - * capture} factory method: argument:

- * class MyTest {
- *
- *   @RegisterExtension
- *   CapturedOutput output = OutputExtension.capture();
- *
- *   @Test
- *   void test() {
- *       assertThat(output).contains("ok");
- *   }
+ *     @Test
+ *     void test(CapturedOutput output) {
+ *         assertThat(output).contains("ok");
+ *     }
  *
  * }
  * 
* * @author Madhura Bhave * @author Phillip Webb + * @author Andy Wilkinson * @since 2.2.0 * @see CapturedOutput */ -public class OutputExtension extends CapturedOutput implements BeforeAllCallback, - AfterAllCallback, BeforeEachCallback, AfterEachCallback, ParameterResolver { +public class OutputCaptureExtension implements BeforeAllCallback, AfterAllCallback, + BeforeEachCallback, AfterEachCallback, ParameterResolver { - OutputExtension() { + private final OutputCapture outputCapture = new OutputCapture(); + + OutputCaptureExtension() { // Package private to prevent users from directly creating an instance. } @Override public void beforeAll(ExtensionContext context) throws Exception { - push(); + this.outputCapture.push(); } @Override public void afterAll(ExtensionContext context) throws Exception { - pop(); + this.outputCapture.pop(); } @Override public void beforeEach(ExtensionContext context) throws Exception { - push(); + this.outputCapture.push(); } @Override public void afterEach(ExtensionContext context) throws Exception { - pop(); + this.outputCapture.pop(); } @Override @@ -104,15 +92,7 @@ public class OutputExtension extends CapturedOutput implements BeforeAllCallback @Override public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { - return this; - } - - /** - * Factory method for use with {@link RegisterExtension @RegisterExtension} fields. - * @return a new {@link CapturedOutput} instance - */ - public static CapturedOutput capture() { - return new OutputExtension(); + return this.outputCapture; } } diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/system/OutputCaptureRule.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/system/OutputCaptureRule.java new file mode 100644 index 0000000000..29db81075b --- /dev/null +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/system/OutputCaptureRule.java @@ -0,0 +1,91 @@ +/* + * Copyright 2012-2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.test.system; + +import java.util.ArrayList; +import java.util.List; + +import org.hamcrest.Matcher; +import org.junit.Assert; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +import static org.hamcrest.Matchers.allOf; + +/** + * JUnit {@code @Rule} to capture output from System.out and System.err. + * + * @author Phillip Webb + * @author Andy Wilkinson + * @since 2.2.0 + */ +public class OutputCaptureRule implements TestRule { + + private final org.springframework.boot.test.system.OutputCapture delegate = new org.springframework.boot.test.system.OutputCapture(); + + private List> matchers = new ArrayList<>(); + + @Override + public Statement apply(Statement base, Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + OutputCaptureRule.this.delegate.push(); + try { + base.evaluate(); + } + finally { + try { + if (!OutputCaptureRule.this.matchers.isEmpty()) { + String output = OutputCaptureRule.this.delegate.toString(); + Assert.assertThat(output, + allOf(OutputCaptureRule.this.matchers)); + } + } + finally { + OutputCaptureRule.this.delegate.pop(); + } + } + } + }; + } + + /** + * Resets the current capture session, clearing its captured output. + * @deprecated since 2.2 with no replacement + */ + @Deprecated + public void reset() { + OutputCaptureRule.this.delegate.reset(); + } + + @Override + public String toString() { + return this.delegate.toString(); + } + + /** + * Verify that the output is matched by the supplied {@code matcher}. Verification is + * performed after the test method has executed. + * @param matcher the matcher + */ + public void expect(Matcher matcher) { + this.matchers.add(matcher); + } + +} diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/extension/package-info.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/system/package-info.java similarity index 83% rename from spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/extension/package-info.java rename to spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/system/package-info.java index b86405f43f..95345c57ea 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/extension/package-info.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/system/package-info.java @@ -15,6 +15,6 @@ */ /** - * JUnit 5 {@link org.junit.jupiter.api.extension.Extension Extensions}. + * Classes for {@link java.lang.System System}-related testing. */ -package org.springframework.boot.test.extension; +package org.springframework.boot.test.system; diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/json/DuplicateJsonObjectContextCustomizerFactoryTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/json/DuplicateJsonObjectContextCustomizerFactoryTests.java index af1a7f2c0a..26876ad7ef 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/json/DuplicateJsonObjectContextCustomizerFactoryTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/json/DuplicateJsonObjectContextCustomizerFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,7 @@ import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.system.OutputCaptureRule; import org.springframework.boot.testsupport.runner.classpath.ClassPathOverrides; import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner; @@ -36,7 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat; public class DuplicateJsonObjectContextCustomizerFactoryTests { @Rule - public OutputCapture output = new OutputCapture(); + public OutputCaptureRule output = new OutputCaptureRule(); @Test public void warningForMultipleVersions() { diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/rule/OutputCaptureTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/rule/OutputCaptureTests.java index c090429a0b..4d8ff8db3c 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/rule/OutputCaptureTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/rule/OutputCaptureTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,6 +26,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Roland Weisleder */ +@Deprecated public class OutputCaptureTests { @Rule diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/extension/OutputExtensionRegisterExtensionTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/system/OutputCaptureRuleTests.java similarity index 58% rename from spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/extension/OutputExtensionRegisterExtensionTests.java rename to spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/system/OutputCaptureRuleTests.java index 785eb65fd6..b945e75a87 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/extension/OutputExtensionRegisterExtensionTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/system/OutputCaptureRuleTests.java @@ -13,29 +13,28 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.boot.test.extension; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +package org.springframework.boot.test.system; + +import org.junit.Rule; +import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; /** - * Tests for {@link OutputExtension} when used via - * {@link RegisterExtension @RegisterExtension}. + * Tests for {@link OutputCaptureRule}. * - * @author Madhura Bhave + * @author Roland Weisleder */ -class OutputExtensionRegisterExtensionTests { +public class OutputCaptureRuleTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); + @Rule + public OutputCaptureRule outputCapture = new OutputCaptureRule(); @Test - void captureShouldReturnAllCapturedOutput() { + public void toStringShouldReturnAllCapturedOutput() { System.out.println("Hello World"); - System.err.println("Error!!!"); - assertThat(this.output).contains("Hello World").contains("Error!!!"); + assertThat(this.outputCapture.toString()).contains("Hello World"); } } diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/extension/CapturedOutputTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/system/OutputCaptureTests.java similarity index 96% rename from spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/extension/CapturedOutputTests.java rename to spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/system/OutputCaptureTests.java index 489c4e8641..3bb471196c 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/extension/CapturedOutputTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/system/OutputCaptureTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.boot.test.extension; +package org.springframework.boot.test.system; import java.io.ByteArrayOutputStream; import java.io.PrintStream; @@ -28,11 +28,11 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** - * Tests for {@link CapturedOutput}. + * Tests for {@link OutputCapture}. * * @author Phillip Webb */ -class CapturedOutputTests { +class OutputCaptureTests { private PrintStream originalOut; @@ -42,7 +42,7 @@ class CapturedOutputTests { private TestPrintStream systemErr; - private CapturedOutput output = new CapturedOutput(); + private OutputCapture output = new OutputCapture(); @BeforeEach void replaceSystemStreams() { diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/extension/OutputExtensionExtendWithTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/system/OutputExtensionExtendWithTests.java similarity index 90% rename from spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/extension/OutputExtensionExtendWithTests.java rename to spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/system/OutputExtensionExtendWithTests.java index f5f1d61674..a021da6f47 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/extension/OutputExtensionExtendWithTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/system/OutputExtensionExtendWithTests.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.springframework.boot.test.extension; +package org.springframework.boot.test.system; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.BeforeAllCallback; @@ -23,11 +23,11 @@ import org.junit.jupiter.api.extension.ExtensionContext; import static org.assertj.core.api.Assertions.assertThat; /** - * Tests for {@link OutputExtension} when used via {@link ExtendWith @ExtendWith}. + * Tests for {@link OutputCaptureExtension} when used via {@link ExtendWith @ExtendWith}. * * @author Madhura Bhave */ -@ExtendWith(OutputExtension.class) +@ExtendWith(OutputCaptureExtension.class) @ExtendWith(OutputExtensionExtendWithTests.BeforeAllExtension.class) class OutputExtensionExtendWithTests { diff --git a/spring-boot-samples/spring-boot-sample-activemq/src/test/java/sample/activemq/SampleActiveMqTests.java b/spring-boot-samples/spring-boot-sample-activemq/src/test/java/sample/activemq/SampleActiveMqTests.java index fa8b60cdff..41f0d4cf15 100644 --- a/spring-boot-samples/spring-boot-sample-activemq/src/test/java/sample/activemq/SampleActiveMqTests.java +++ b/spring-boot-samples/spring-boot-sample-activemq/src/test/java/sample/activemq/SampleActiveMqTests.java @@ -17,12 +17,12 @@ package sample.activemq; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import static org.assertj.core.api.Assertions.assertThat; @@ -32,19 +32,17 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Eddú Meléndez */ @SpringBootTest +@ExtendWith(OutputCaptureExtension.class) class SampleActiveMqTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Autowired private Producer producer; @Test - void sendSimpleMessage() throws InterruptedException { + void sendSimpleMessage(CapturedOutput capturedOutput) throws InterruptedException { this.producer.send("Test message"); Thread.sleep(1000L); - assertThat(this.output.toString().contains("Test message")).isTrue(); + assertThat(capturedOutput).contains("Test message"); } } diff --git a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/test/java/sample/actuator/log4j2/SampleActuatorLog4J2ApplicationTests.java b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/test/java/sample/actuator/log4j2/SampleActuatorLog4J2ApplicationTests.java index f513ae0ad7..8da9e43e68 100644 --- a/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/test/java/sample/actuator/log4j2/SampleActuatorLog4J2ApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-actuator-log4j2/src/test/java/sample/actuator/log4j2/SampleActuatorLog4J2ApplicationTests.java @@ -21,13 +21,13 @@ import java.util.Base64; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.test.web.servlet.MockMvc; import static org.assertj.core.api.Assertions.assertThat; @@ -44,21 +44,19 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. */ @SpringBootTest @AutoConfigureMockMvc +@ExtendWith(OutputCaptureExtension.class) class SampleActuatorLog4J2ApplicationTests { private static final Logger logger = LogManager .getLogger(SampleActuatorLog4J2ApplicationTests.class); - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Autowired private MockMvc mvc; @Test - void testLogger() { + void testLogger(CapturedOutput capturedOutput) { logger.info("Hello World"); - assertThat(this.output).contains("Hello World"); + assertThat(capturedOutput).contains("Hello World"); } @Test diff --git a/spring-boot-samples/spring-boot-sample-aop/src/test/java/sample/aop/SampleAopApplicationTests.java b/spring-boot-samples/spring-boot-sample-aop/src/test/java/sample/aop/SampleAopApplicationTests.java index 1f4d54f4a9..6c8685b927 100644 --- a/spring-boot-samples/spring-boot-sample-aop/src/test/java/sample/aop/SampleAopApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-aop/src/test/java/sample/aop/SampleAopApplicationTests.java @@ -19,10 +19,10 @@ package sample.aop; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import static org.assertj.core.api.Assertions.assertThat; @@ -32,11 +32,9 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Dave Syer * @author Phillip Webb */ +@ExtendWith(OutputCaptureExtension.class) class SampleAopApplicationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - private String profiles; @BeforeEach @@ -55,15 +53,15 @@ class SampleAopApplicationTests { } @Test - void testDefaultSettings() throws Exception { + void testDefaultSettings(CapturedOutput capturedOutput) throws Exception { SampleAopApplication.main(new String[0]); - assertThat(this.output).contains("Hello Phil"); + assertThat(capturedOutput).contains("Hello Phil"); } @Test - void testCommandLineOverrides() throws Exception { + void testCommandLineOverrides(CapturedOutput capturedOutput) throws Exception { SampleAopApplication.main(new String[] { "--name=Gordon" }); - assertThat(this.output).contains("Hello Gordon"); + assertThat(capturedOutput).contains("Hello Gordon"); } } diff --git a/spring-boot-samples/spring-boot-sample-batch/src/test/java/sample/batch/SampleBatchApplicationTests.java b/spring-boot-samples/spring-boot-sample-batch/src/test/java/sample/batch/SampleBatchApplicationTests.java index 7adbbdec0d..3e165a8e8c 100644 --- a/spring-boot-samples/spring-boot-sample-batch/src/test/java/sample/batch/SampleBatchApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-batch/src/test/java/sample/batch/SampleBatchApplicationTests.java @@ -17,24 +17,22 @@ package sample.batch; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.SpringApplication; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import static org.assertj.core.api.Assertions.assertThat; +@ExtendWith(OutputCaptureExtension.class) class SampleBatchApplicationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test - void testDefaultSettings() { + void testDefaultSettings(CapturedOutput capturedOutput) { assertThat(SpringApplication .exit(SpringApplication.run(SampleBatchApplication.class))).isEqualTo(0); - assertThat(this.output).contains("completed with the following parameters"); + assertThat(capturedOutput).contains("completed with the following parameters"); } } diff --git a/spring-boot-samples/spring-boot-sample-data-cassandra/src/test/java/sample/data/cassandra/SampleCassandraApplicationTests.java b/spring-boot-samples/spring-boot-sample-data-cassandra/src/test/java/sample/data/cassandra/SampleCassandraApplicationTests.java index bc19fd14b5..7b61ac1ea2 100644 --- a/spring-boot-samples/spring-boot-sample-data-cassandra/src/test/java/sample/data/cassandra/SampleCassandraApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-data-cassandra/src/test/java/sample/data/cassandra/SampleCassandraApplicationTests.java @@ -25,8 +25,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.TestExecutionListeners.MergeMode; @@ -37,7 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat; */ @TestExecutionListeners(mergeMode = MergeMode.MERGE_WITH_DEFAULTS, listeners = { OrderedCassandraTestExecutionListener.class }) -@ExtendWith(OutputExtension.class) +@ExtendWith(OutputCaptureExtension.class) @SpringBootTest @CassandraDataSet(keyspace = "mykeyspace", value = "setup.cql") @EmbeddedCassandra(timeout = 60000) diff --git a/spring-boot-samples/spring-boot-sample-data-couchbase/src/test/java/sample/data/couchbase/SampleCouchbaseApplicationTests.java b/spring-boot-samples/spring-boot-sample-data-couchbase/src/test/java/sample/data/couchbase/SampleCouchbaseApplicationTests.java index 90380771fb..4dfd27acbb 100644 --- a/spring-boot-samples/spring-boot-sample-data-couchbase/src/test/java/sample/data/couchbase/SampleCouchbaseApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-data-couchbase/src/test/java/sample/data/couchbase/SampleCouchbaseApplicationTests.java @@ -18,22 +18,20 @@ package sample.data.couchbase; import java.net.ConnectException; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.core.NestedCheckedException; import static org.assertj.core.api.Assertions.assertThat; +@ExtendWith(OutputCaptureExtension.class) class SampleCouchbaseApplicationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test - void testDefaultSettings() { + void testDefaultSettings(CapturedOutput capturedOutput) { try { new SpringApplicationBuilder(SampleCouchbaseApplication.class) .run("--server.port=0"); @@ -43,7 +41,7 @@ class SampleCouchbaseApplicationTests { return; } } - assertThat(this.output).contains("firstName='Alice', lastName='Smith'"); + assertThat(capturedOutput).contains("firstName='Alice', lastName='Smith'"); } private boolean serverNotRunning(RuntimeException ex) { diff --git a/spring-boot-samples/spring-boot-sample-data-elasticsearch/src/test/java/sample/data/elasticsearch/SampleElasticsearchApplicationTests.java b/spring-boot-samples/spring-boot-sample-data-elasticsearch/src/test/java/sample/data/elasticsearch/SampleElasticsearchApplicationTests.java index eba2d225c1..97b15126bf 100644 --- a/spring-boot-samples/spring-boot-sample-data-elasticsearch/src/test/java/sample/data/elasticsearch/SampleElasticsearchApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-data-elasticsearch/src/test/java/sample/data/elasticsearch/SampleElasticsearchApplicationTests.java @@ -18,11 +18,11 @@ package sample.data.elasticsearch; import org.elasticsearch.client.transport.NoNodeAvailableException; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import static org.assertj.core.api.Assertions.assertThat; @@ -31,13 +31,11 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Artur Konczak */ +@ExtendWith(OutputCaptureExtension.class) class SampleElasticsearchApplicationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test - void testDefaultSettings() { + void testDefaultSettings(CapturedOutput capturedOutput) { try { new SpringApplicationBuilder(SampleElasticsearchApplication.class).run(); } @@ -47,7 +45,7 @@ class SampleElasticsearchApplicationTests { } throw ex; } - assertThat(this.output).contains("firstName='Alice', lastName='Smith'"); + assertThat(capturedOutput).contains("firstName='Alice', lastName='Smith'"); } private boolean elasticsearchRunning(Exception ex) { diff --git a/spring-boot-samples/spring-boot-sample-data-ldap/src/test/java/sample/data/ldap/SampleLdapApplicationTests.java b/spring-boot-samples/spring-boot-sample-data-ldap/src/test/java/sample/data/ldap/SampleLdapApplicationTests.java index d7a6b090bf..90d26a60ef 100644 --- a/spring-boot-samples/spring-boot-sample-data-ldap/src/test/java/sample/data/ldap/SampleLdapApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-data-ldap/src/test/java/sample/data/ldap/SampleLdapApplicationTests.java @@ -20,8 +20,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import static org.assertj.core.api.Assertions.assertThat; @@ -30,7 +30,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Phillip Webb */ -@ExtendWith(OutputExtension.class) +@ExtendWith(OutputCaptureExtension.class) @SpringBootTest class SampleLdapApplicationTests { diff --git a/spring-boot-samples/spring-boot-sample-data-mongodb/src/main/java/sample/data/mongo/SampleMongoApplication.java b/spring-boot-samples/spring-boot-sample-data-mongodb/src/main/java/sample/data/mongo/SampleMongoApplication.java index 04f78b2286..7521a87a89 100644 --- a/spring-boot-samples/spring-boot-sample-data-mongodb/src/main/java/sample/data/mongo/SampleMongoApplication.java +++ b/spring-boot-samples/spring-boot-sample-data-mongodb/src/main/java/sample/data/mongo/SampleMongoApplication.java @@ -16,10 +16,14 @@ package sample.data.mongo; +import java.util.concurrent.TimeUnit; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.mongo.MongoClientSettingsBuilderCustomizer; +import org.springframework.context.annotation.Bean; @SpringBootApplication public class SampleMongoApplication implements CommandLineRunner { @@ -55,6 +59,13 @@ public class SampleMongoApplication implements CommandLineRunner { } } + @Bean + public MongoClientSettingsBuilderCustomizer customizer() { + return (builder) -> builder + .applyToConnectionPoolSettings((connectionPool) -> connectionPool + .maxConnectionIdleTime(5, TimeUnit.MINUTES)); + } + public static void main(String[] args) { SpringApplication.run(SampleMongoApplication.class, args); } diff --git a/spring-boot-samples/spring-boot-sample-data-mongodb/src/test/java/sample/data/mongo/SampleMongoApplicationTests.java b/spring-boot-samples/spring-boot-sample-data-mongodb/src/test/java/sample/data/mongo/SampleMongoApplicationTests.java index af1931a6f0..3ff624cc48 100644 --- a/spring-boot-samples/spring-boot-sample-data-mongodb/src/test/java/sample/data/mongo/SampleMongoApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-data-mongodb/src/test/java/sample/data/mongo/SampleMongoApplicationTests.java @@ -20,8 +20,8 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import static org.assertj.core.api.Assertions.assertThat; @@ -31,7 +31,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Dave Syer * @author Andy Wilkinson */ -@ExtendWith(OutputExtension.class) +@ExtendWith(OutputCaptureExtension.class) @SpringBootTest class SampleMongoApplicationTests { diff --git a/spring-boot-samples/spring-boot-sample-data-neo4j/src/test/java/sample/data/neo4j/SampleNeo4jApplicationTests.java b/spring-boot-samples/spring-boot-sample-data-neo4j/src/test/java/sample/data/neo4j/SampleNeo4jApplicationTests.java index a84b56936d..513b76af27 100644 --- a/spring-boot-samples/spring-boot-sample-data-neo4j/src/test/java/sample/data/neo4j/SampleNeo4jApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-data-neo4j/src/test/java/sample/data/neo4j/SampleNeo4jApplicationTests.java @@ -17,11 +17,11 @@ package sample.data.neo4j; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.neo4j.driver.v1.exceptions.ServiceUnavailableException; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import static org.assertj.core.api.Assertions.assertThat; @@ -30,13 +30,11 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Stephane Nicoll */ +@ExtendWith(OutputCaptureExtension.class) class SampleNeo4jApplicationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test - void testDefaultSettings() { + void testDefaultSettings(CapturedOutput capturedOutput) { try { SampleNeo4jApplication.main(new String[0]); } @@ -45,7 +43,7 @@ class SampleNeo4jApplicationTests { return; } } - assertThat(this.output).contains("firstName='Alice', lastName='Smith'"); + assertThat(capturedOutput).contains("firstName='Alice', lastName='Smith'"); } private boolean neo4jServerRunning(Throwable ex) { diff --git a/spring-boot-samples/spring-boot-sample-data-redis/src/test/java/sample/data/redis/SampleRedisApplicationTests.java b/spring-boot-samples/spring-boot-sample-data-redis/src/test/java/sample/data/redis/SampleRedisApplicationTests.java index 3e9523ca19..2b852d7cf5 100644 --- a/spring-boot-samples/spring-boot-sample-data-redis/src/test/java/sample/data/redis/SampleRedisApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-data-redis/src/test/java/sample/data/redis/SampleRedisApplicationTests.java @@ -17,10 +17,10 @@ package sample.data.redis; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.data.redis.RedisConnectionFailureException; import static org.assertj.core.api.Assertions.assertThat; @@ -30,13 +30,11 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Dave Syer */ +@ExtendWith(OutputCaptureExtension.class) class SampleRedisApplicationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test - void testDefaultSettings() { + void testDefaultSettings(CapturedOutput capturedOutput) { try { SampleRedisApplication.main(new String[0]); } @@ -45,7 +43,7 @@ class SampleRedisApplicationTests { return; } } - assertThat(this.output).contains("Found key spring.boot.redis.test"); + assertThat(capturedOutput).contains("Found key spring.boot.redis.test"); } private boolean redisServerRunning(Throwable ex) { diff --git a/spring-boot-samples/spring-boot-sample-data-solr/src/test/java/sample/data/solr/SampleSolrApplicationTests.java b/spring-boot-samples/spring-boot-sample-data-solr/src/test/java/sample/data/solr/SampleSolrApplicationTests.java index 1646d809f7..78afa4183b 100644 --- a/spring-boot-samples/spring-boot-sample-data-solr/src/test/java/sample/data/solr/SampleSolrApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-data-solr/src/test/java/sample/data/solr/SampleSolrApplicationTests.java @@ -17,21 +17,19 @@ package sample.data.solr; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.core.NestedCheckedException; import static org.assertj.core.api.Assertions.assertThat; +@ExtendWith(OutputCaptureExtension.class) class SampleSolrApplicationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test - void testDefaultSettings() throws Exception { + void testDefaultSettings(CapturedOutput capturedOutput) throws Exception { try { SampleSolrApplication.main(new String[0]); } @@ -40,7 +38,7 @@ class SampleSolrApplicationTests { return; } } - assertThat(this.output).contains("name=Sony Playstation"); + assertThat(capturedOutput).contains("name=Sony Playstation"); } @SuppressWarnings("serial") diff --git a/spring-boot-samples/spring-boot-sample-jooq/src/test/java/sample/jooq/SampleJooqApplicationTests.java b/spring-boot-samples/spring-boot-sample-jooq/src/test/java/sample/jooq/SampleJooqApplicationTests.java index db965ca53e..86a9a548e5 100644 --- a/spring-boot-samples/spring-boot-sample-jooq/src/test/java/sample/jooq/SampleJooqApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-jooq/src/test/java/sample/jooq/SampleJooqApplicationTests.java @@ -17,29 +17,26 @@ package sample.jooq; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import static org.assertj.core.api.Assertions.assertThat; /** * Integration tests for {@link SampleJooqApplication}. */ +@ExtendWith(OutputCaptureExtension.class) class SampleJooqApplicationTests { private static final String[] NO_ARGS = {}; - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test - void outputResults() { + void outputResults(CapturedOutput capturedOutput) { SampleJooqApplication.main(NO_ARGS); - assertThat(this.output).contains("jOOQ Fetch 1 Greg Turnquest"); - assertThat(this.output).contains("jOOQ Fetch 2 Craig Walls"); - assertThat(this.output) + assertThat(capturedOutput).contains("jOOQ Fetch 1 Greg Turnquest") + .contains("jOOQ Fetch 2 Craig Walls") .contains("jOOQ SQL " + "[Learning Spring Boot : Greg Turnquest, " + "Spring Boot in Action : Craig Walls]"); } diff --git a/spring-boot-samples/spring-boot-sample-jta-atomikos/src/test/java/sample/atomikos/SampleAtomikosApplicationTests.java b/spring-boot-samples/spring-boot-sample-jta-atomikos/src/test/java/sample/atomikos/SampleAtomikosApplicationTests.java index a2e30c82b2..eb4759fcfb 100644 --- a/spring-boot-samples/spring-boot-sample-jta-atomikos/src/test/java/sample/atomikos/SampleAtomikosApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-jta-atomikos/src/test/java/sample/atomikos/SampleAtomikosApplicationTests.java @@ -18,10 +18,10 @@ package sample.atomikos; import org.assertj.core.api.Condition; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import static org.assertj.core.api.Assertions.assertThat; @@ -30,18 +30,15 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Phillip Webb */ +@ExtendWith(OutputCaptureExtension.class) class SampleAtomikosApplicationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test - void testTransactionRollback() throws Exception { + void testTransactionRollback(CapturedOutput capturedOutput) throws Exception { SampleAtomikosApplication.main(new String[] {}); - assertThat(this.output.toString()).has(substring(1, "---->")); - assertThat(this.output.toString()).has(substring(1, "----> josh")); - assertThat(this.output.toString()).has(substring(2, "Count is 1")); - assertThat(this.output.toString()).has(substring(1, "Simulated error")); + assertThat(capturedOutput.toString()).has(substring(1, "---->")) + .has(substring(1, "----> josh")).has(substring(2, "Count is 1")) + .has(substring(1, "Simulated error")); } private Condition substring(int times, String substring) { diff --git a/spring-boot-samples/spring-boot-sample-jta-bitronix/src/test/java/sample/bitronix/SampleBitronixApplicationTests.java b/spring-boot-samples/spring-boot-sample-jta-bitronix/src/test/java/sample/bitronix/SampleBitronixApplicationTests.java index 5c2ffc45ee..2b2167f4f8 100644 --- a/spring-boot-samples/spring-boot-sample-jta-bitronix/src/test/java/sample/bitronix/SampleBitronixApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-jta-bitronix/src/test/java/sample/bitronix/SampleBitronixApplicationTests.java @@ -19,11 +19,11 @@ package sample.bitronix; import bitronix.tm.resource.jms.PoolingConnectionFactory; import org.assertj.core.api.Condition; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.SpringApplication; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.context.ApplicationContext; import static org.assertj.core.api.Assertions.assertThat; @@ -33,18 +33,15 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Phillip Webb */ +@ExtendWith(OutputCaptureExtension.class) class SampleBitronixApplicationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test - void testTransactionRollback() throws Exception { + void testTransactionRollback(CapturedOutput capturedOutput) throws Exception { SampleBitronixApplication.main(new String[] {}); - assertThat(this.output.toString()).has(substring(1, "---->")); - assertThat(this.output.toString()).has(substring(1, "----> josh")); - assertThat(this.output.toString()).has(substring(2, "Count is 1")); - assertThat(this.output.toString()).has(substring(1, "Simulated error")); + assertThat(capturedOutput.toString()).has(substring(1, "---->")) + .has(substring(1, "----> josh")).has(substring(2, "Count is 1")) + .has(substring(1, "Simulated error")); } @Test diff --git a/spring-boot-samples/spring-boot-sample-liquibase/src/test/java/sample/liquibase/SampleLiquibaseApplicationTests.java b/spring-boot-samples/spring-boot-sample-liquibase/src/test/java/sample/liquibase/SampleLiquibaseApplicationTests.java index 3ba6699abd..b0db276f31 100644 --- a/spring-boot-samples/spring-boot-sample-liquibase/src/test/java/sample/liquibase/SampleLiquibaseApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-liquibase/src/test/java/sample/liquibase/SampleLiquibaseApplicationTests.java @@ -19,21 +19,19 @@ package sample.liquibase; import java.net.ConnectException; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.core.NestedCheckedException; import static org.assertj.core.api.Assertions.assertThat; +@ExtendWith(OutputCaptureExtension.class) class SampleLiquibaseApplicationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test - void testDefaultSettings() throws Exception { + void testDefaultSettings(CapturedOutput capturedOutput) throws Exception { try { SampleLiquibaseApplication.main(new String[] { "--server.port=0" }); } @@ -42,7 +40,7 @@ class SampleLiquibaseApplicationTests { return; } } - assertThat(this.output).contains("Successfully acquired change log lock") + assertThat(capturedOutput).contains("Successfully acquired change log lock") .contains("Creating database history " + "table with name: PUBLIC.DATABASECHANGELOG") .contains("Table person created") diff --git a/spring-boot-samples/spring-boot-sample-logback/src/test/java/sample/logback/SampleLogbackApplicationTests.java b/spring-boot-samples/spring-boot-sample-logback/src/test/java/sample/logback/SampleLogbackApplicationTests.java index dc3cd9676f..1401a46087 100644 --- a/spring-boot-samples/spring-boot-sample-logback/src/test/java/sample/logback/SampleLogbackApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-logback/src/test/java/sample/logback/SampleLogbackApplicationTests.java @@ -17,31 +17,29 @@ package sample.logback; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import static org.assertj.core.api.Assertions.assertThat; +@ExtendWith(OutputCaptureExtension.class) class SampleLogbackApplicationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test - void testLoadedCustomLogbackConfig() throws Exception { + void testLoadedCustomLogbackConfig(CapturedOutput capturedOutput) throws Exception { SampleLogbackApplication.main(new String[0]); - assertThat(this.output).contains("Sample Debug Message"); - assertThat(this.output).doesNotContain("Sample Trace Message"); + assertThat(capturedOutput).contains("Sample Debug Message") + .doesNotContain("Sample Trace Message"); } @Test - void testProfile() throws Exception { + void testProfile(CapturedOutput capturedOutput) throws Exception { SampleLogbackApplication .main(new String[] { "--spring.profiles.active=staging" }); - assertThat(this.output).contains("Sample Debug Message"); - assertThat(this.output).contains("Sample Trace Message"); + assertThat(capturedOutput).contains("Sample Debug Message") + .contains("Sample Trace Message"); } } diff --git a/spring-boot-samples/spring-boot-sample-profile/src/test/java/sample/profile/SampleProfileApplicationTests.java b/spring-boot-samples/spring-boot-sample-profile/src/test/java/sample/profile/SampleProfileApplicationTests.java index 63665eb7c2..89e70ccf2f 100644 --- a/spring-boot-samples/spring-boot-sample-profile/src/test/java/sample/profile/SampleProfileApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-profile/src/test/java/sample/profile/SampleProfileApplicationTests.java @@ -19,18 +19,16 @@ package sample.profile; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import static org.assertj.core.api.Assertions.assertThat; +@ExtendWith(OutputCaptureExtension.class) class SampleProfileApplicationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - private String profiles; @BeforeEach @@ -49,20 +47,20 @@ class SampleProfileApplicationTests { } @Test - void testDefaultProfile() throws Exception { + void testDefaultProfile(CapturedOutput capturedOutput) { SampleProfileApplication.main(new String[0]); - assertThat(this.output).contains("Hello Phil"); + assertThat(capturedOutput).contains("Hello Phil"); } @Test - void testGoodbyeProfile() throws Exception { + void testGoodbyeProfile(CapturedOutput capturedOutput) { System.setProperty("spring.profiles.active", "goodbye"); SampleProfileApplication.main(new String[0]); - assertThat(this.output).contains("Goodbye Everyone"); + assertThat(capturedOutput).contains("Goodbye Everyone"); } @Test - void testGenericProfile() throws Exception { + void testGenericProfile(CapturedOutput capturedOutput) { /* * This is a profile that requires a new environment property, and one which is * only overridden in the current working directory. That file also only contains @@ -71,14 +69,14 @@ class SampleProfileApplicationTests { */ System.setProperty("spring.profiles.active", "generic"); SampleProfileApplication.main(new String[0]); - assertThat(this.output).contains("Bonjour Phil"); + assertThat(capturedOutput).contains("Bonjour Phil"); } @Test - void testGoodbyeProfileFromCommandline() throws Exception { + void testGoodbyeProfileFromCommandline(CapturedOutput capturedOutput) { SampleProfileApplication .main(new String[] { "--spring.profiles.active=goodbye" }); - assertThat(this.output).contains("Goodbye Everyone"); + assertThat(capturedOutput).contains("Goodbye Everyone"); } } diff --git a/spring-boot-samples/spring-boot-sample-quartz/src/test/java/sample/quartz/SampleQuartzApplicationTests.java b/spring-boot-samples/spring-boot-sample-quartz/src/test/java/sample/quartz/SampleQuartzApplicationTests.java index f94a229568..bd940e78b8 100644 --- a/spring-boot-samples/spring-boot-sample-quartz/src/test/java/sample/quartz/SampleQuartzApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-quartz/src/test/java/sample/quartz/SampleQuartzApplicationTests.java @@ -17,11 +17,11 @@ package sample.quartz; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.SpringApplication; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.context.ConfigurableApplicationContext; import static org.assertj.core.api.Assertions.assertThat; @@ -31,21 +31,19 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Eddú Meléndez */ +@ExtendWith(OutputCaptureExtension.class) class SampleQuartzApplicationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test - void quartzJobIsTriggered() throws InterruptedException { + void quartzJobIsTriggered(CapturedOutput capturedOutput) throws InterruptedException { try (ConfigurableApplicationContext context = SpringApplication .run(SampleQuartzApplication.class)) { long end = System.currentTimeMillis() + 5000; - while ((!this.output.toString().contains("Hello World!")) + while ((!capturedOutput.toString().contains("Hello World!")) && System.currentTimeMillis() < end) { Thread.sleep(100); } - assertThat(this.output).contains("Hello World!"); + assertThat(capturedOutput).contains("Hello World!"); } } diff --git a/spring-boot-samples/spring-boot-sample-simple/src/test/java/sample/simple/SampleSimpleApplicationTests.java b/spring-boot-samples/spring-boot-sample-simple/src/test/java/sample/simple/SampleSimpleApplicationTests.java index 2cca289021..3e75ca8887 100644 --- a/spring-boot-samples/spring-boot-sample-simple/src/test/java/sample/simple/SampleSimpleApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-simple/src/test/java/sample/simple/SampleSimpleApplicationTests.java @@ -19,10 +19,10 @@ package sample.simple; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import static org.assertj.core.api.Assertions.assertThat; @@ -32,11 +32,9 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Dave Syer * @author Phillip Webb */ +@ExtendWith(OutputCaptureExtension.class) class SampleSimpleApplicationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - private String profiles; @BeforeEach @@ -55,17 +53,15 @@ class SampleSimpleApplicationTests { } @Test - void testDefaultSettings() throws Exception { + void testDefaultSettings(CapturedOutput capturedOutput) { SampleSimpleApplication.main(new String[0]); - String output = this.output.toString(); - assertThat(output).contains("Hello Phil"); + assertThat(capturedOutput).contains("Hello Phil"); } @Test - void testCommandLineOverrides() throws Exception { + void testCommandLineOverrides(CapturedOutput capturedOutput) { SampleSimpleApplication.main(new String[] { "--name=Gordon", "--duration=1m" }); - String output = this.output.toString(); - assertThat(output).contains("Hello Gordon for 60 seconds"); + assertThat(capturedOutput).contains("Hello Gordon for 60 seconds"); } } diff --git a/spring-boot-samples/spring-boot-sample-webservices/src/test/java/sample/webservices/SampleWsApplicationTests.java b/spring-boot-samples/spring-boot-sample-webservices/src/test/java/sample/webservices/SampleWsApplicationTests.java index c6b95084f4..b7654fc022 100644 --- a/spring-boot-samples/spring-boot-sample-webservices/src/test/java/sample/webservices/SampleWsApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-webservices/src/test/java/sample/webservices/SampleWsApplicationTests.java @@ -23,23 +23,21 @@ import javax.xml.transform.stream.StreamSource; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.boot.web.server.LocalServerPort; import org.springframework.ws.client.core.WebServiceTemplate; import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) +@ExtendWith(OutputCaptureExtension.class) class SampleWsApplicationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - private WebServiceTemplate webServiceTemplate = new WebServiceTemplate(); @LocalServerPort @@ -52,7 +50,7 @@ class SampleWsApplicationTests { } @Test - void testSendingHolidayRequest() { + void testSendingHolidayRequest(CapturedOutput capturedOutput) { final String request = "" + " " + " 2013-10-20" + " 2013-11-22" + " " @@ -63,7 +61,7 @@ class SampleWsApplicationTests { StreamSource source = new StreamSource(new StringReader(request)); StreamResult result = new StreamResult(System.out); this.webServiceTemplate.sendSourceAndReceiveToResult(source, result); - assertThat(this.output.toString()).contains("Booking holiday for"); + assertThat(capturedOutput).contains("Booking holiday for"); } } diff --git a/spring-boot-samples/spring-boot-sample-xml/src/test/java/sample/xml/SampleSpringXmlApplicationTests.java b/spring-boot-samples/spring-boot-sample-xml/src/test/java/sample/xml/SampleSpringXmlApplicationTests.java index cd90885b99..c3f27c40d2 100644 --- a/spring-boot-samples/spring-boot-sample-xml/src/test/java/sample/xml/SampleSpringXmlApplicationTests.java +++ b/spring-boot-samples/spring-boot-sample-xml/src/test/java/sample/xml/SampleSpringXmlApplicationTests.java @@ -17,23 +17,20 @@ package sample.xml; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.boot.test.extension.CapturedOutput; -import org.springframework.boot.test.extension.OutputExtension; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import static org.assertj.core.api.Assertions.assertThat; +@ExtendWith(OutputCaptureExtension.class) class SampleSpringXmlApplicationTests { - @RegisterExtension - CapturedOutput output = OutputExtension.capture(); - @Test - void testDefaultSettings() throws Exception { + void testDefaultSettings(CapturedOutput capturedOutput) throws Exception { SampleSpringXmlApplication.main(new String[0]); - String output = this.output.toString(); - assertThat(output).contains("Hello World"); + assertThat(capturedOutput).contains("Hello World"); } }