From 8d4a2add6306a13e5a51068869559f4610330b8b Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sun, 14 Jul 2019 19:10:02 +0100 Subject: [PATCH] Allow OutputCaptureRule to be used with AssertJ Update `OutputCaptureRule` so that it now implements `CapturedOutput` and can be used directly with AssertJ. Closes gh-17512 --- .../boot/test/system/OutputCaptureRule.java | 34 ++++++++++++++++++- .../test/system/OutputCaptureRuleTests.java | 10 ++++-- .../testsupport/system/OutputCaptureRule.java | 17 +++++++++- 3 files changed, 57 insertions(+), 4 deletions(-) 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 index b6d1ad8d5d..9dcb6698e3 100644 --- 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 @@ -21,6 +21,7 @@ import java.util.List; import org.hamcrest.Matcher; import org.junit.Assert; +import org.junit.Rule; import org.junit.rules.TestRule; import org.junit.runner.Description; import org.junit.runners.model.Statement; @@ -29,12 +30,28 @@ import static org.hamcrest.Matchers.allOf; /** * JUnit {@code @Rule} to capture output from System.out and System.err. + *

+ * To use add as a {@link Rule @Rule}: + * + *

+ * public class MyTest {
+ *
+ *     @Rule
+ *     public OutputCaptureRule output = new OutputCaptureRule();
+ *
+ *     @Test
+ *     public void test() {
+ *         assertThat(output).contains("ok");
+ *     }
+ *
+ * }
+ * 
* * @author Phillip Webb * @author Andy Wilkinson * @since 2.2.0 */ -public class OutputCaptureRule implements TestRule { +public class OutputCaptureRule implements TestRule, CapturedOutput { private final OutputCapture delegate = new OutputCapture(); @@ -73,6 +90,21 @@ public class OutputCaptureRule implements TestRule { OutputCaptureRule.this.delegate.reset(); } + @Override + public String getAll() { + return this.delegate.getAll(); + } + + @Override + public String getOut() { + return this.delegate.getOut(); + } + + @Override + public String getErr() { + return this.delegate.getErr(); + } + @Override public String toString() { return this.delegate.toString(); diff --git a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/system/OutputCaptureRuleTests.java b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/system/OutputCaptureRuleTests.java index b945e75a87..0cc92fbef6 100644 --- a/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/system/OutputCaptureRuleTests.java +++ b/spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/system/OutputCaptureRuleTests.java @@ -29,12 +29,18 @@ import static org.assertj.core.api.Assertions.assertThat; public class OutputCaptureRuleTests { @Rule - public OutputCaptureRule outputCapture = new OutputCaptureRule(); + public OutputCaptureRule output = new OutputCaptureRule(); @Test public void toStringShouldReturnAllCapturedOutput() { System.out.println("Hello World"); - assertThat(this.outputCapture.toString()).contains("Hello World"); + assertThat(this.output.toString()).contains("Hello World"); + } + + @Test + public void captureShouldBeAssertable() { + System.out.println("Hello World"); + assertThat(this.output).contains("Hello World"); } } diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/system/OutputCaptureRule.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/system/OutputCaptureRule.java index abf83106c2..0dcaf139ab 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/system/OutputCaptureRule.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/system/OutputCaptureRule.java @@ -34,7 +34,7 @@ import static org.hamcrest.Matchers.allOf; * @author Andy Wilkinson * @since 2.2.0 */ -public class OutputCaptureRule implements TestRule { +public class OutputCaptureRule implements TestRule, CapturedOutput { private final OutputCapture delegate = new OutputCapture(); @@ -64,6 +64,21 @@ public class OutputCaptureRule implements TestRule { }; } + @Override + public String getAll() { + return this.delegate.getAll(); + } + + @Override + public String getOut() { + return this.delegate.getOut(); + } + + @Override + public String getErr() { + return this.delegate.getErr(); + } + @Override public String toString() { return this.delegate.toString();