From d8e5169f5f1952c5db45b880fc65bafe1fde21e6 Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Thu, 23 May 2019 12:24:12 +0900 Subject: [PATCH] Polish Closes gh-16948 --- .../boot/test/extension/OutputCapture.java | 25 ++----------------- .../OutputCaptureExtendWithTests.java | 10 +++----- .../OutputCaptureRegisterExtensionTests.java | 5 ++-- 3 files changed, 8 insertions(+), 32 deletions(-) diff --git a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/extension/OutputCapture.java b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/extension/OutputCapture.java index 0ead6c4c19..8958b7adf9 100644 --- a/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/extension/OutputCapture.java +++ b/spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/extension/OutputCapture.java @@ -19,11 +19,7 @@ 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.jupiter.api.extension.AfterEachCallback; import org.junit.jupiter.api.extension.BeforeAllCallback; import org.junit.jupiter.api.extension.BeforeEachCallback; @@ -34,12 +30,11 @@ import org.junit.jupiter.api.extension.ParameterResolver; import org.springframework.boot.ansi.AnsiOutput; -import static org.hamcrest.Matchers.allOf; - /** * JUnit5 {@code @Extension} to capture output from System.out and System.err. * * @author Madhura Bhave + * @since 2.2.0 */ public class OutputCapture implements BeforeEachCallback, AfterEachCallback, BeforeAllCallback, ParameterResolver, CharSequence { @@ -52,20 +47,9 @@ public class OutputCapture implements BeforeEachCallback, AfterEachCallback, private ByteArrayOutputStream classLevelCopy; - private List> matchers = new ArrayList<>(); - @Override public void afterEach(ExtensionContext context) { - try { - if (!this.matchers.isEmpty()) { - String output = this.toString(); - Assert.assertThat(output, allOf(this.matchers)); - } - } - finally { - releaseOutput(); - } - + releaseOutput(); } @Override @@ -168,11 +152,6 @@ public class OutputCapture implements BeforeEachCallback, AfterEachCallback, 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); diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/extension/OutputCaptureExtendWithTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/extension/OutputCaptureExtendWithTests.java index b16b911427..13c0a74511 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/extension/OutputCaptureExtendWithTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/extension/OutputCaptureExtendWithTests.java @@ -29,21 +29,19 @@ import static org.assertj.core.api.Assertions.assertThat; */ @ExtendWith(OutputCapture.class) @ExtendWith(OutputCaptureExtendWithTests.BeforeAllExtension.class) -public class OutputCaptureExtendWithTests { +class OutputCaptureExtendWithTests { @Test void captureShouldReturnOutputCapturedBeforeTestMethod(OutputCapture output) { - assertThat(output).contains("Before all"); - assertThat(output).doesNotContain("Hello"); + assertThat(output).contains("Before all").doesNotContain("Hello"); } @Test void captureShouldReturnAllCapturedOutput(OutputCapture output) { System.out.println("Hello World"); System.err.println("Error!!!"); - assertThat(output).contains("Before all"); - assertThat(output).contains("Hello World"); - assertThat(output).contains("Error!!!"); + assertThat(output).contains("Before all").contains("Hello World") + .contains("Error!!!"); } static class BeforeAllExtension implements BeforeAllCallback { diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/extension/OutputCaptureRegisterExtensionTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/extension/OutputCaptureRegisterExtensionTests.java index 9ff38d73d7..b898f0f3b6 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/extension/OutputCaptureRegisterExtensionTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/extension/OutputCaptureRegisterExtensionTests.java @@ -25,7 +25,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Madhura Bhave */ -public class OutputCaptureRegisterExtensionTests { +class OutputCaptureRegisterExtensionTests { @RegisterExtension OutputCapture output = new OutputCapture(); @@ -34,8 +34,7 @@ public class OutputCaptureRegisterExtensionTests { void captureShouldReturnAllCapturedOutput() { System.out.println("Hello World"); System.err.println("Error!!!"); - assertThat(this.output).contains("Hello World"); - assertThat(this.output).contains("Error!!!"); + assertThat(this.output).contains("Hello World").contains("Error!!!"); } }