Commit 8d4a2add authored by Phillip Webb's avatar Phillip Webb

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
parent 79b29396
...@@ -21,6 +21,7 @@ import java.util.List; ...@@ -21,6 +21,7 @@ import java.util.List;
import org.hamcrest.Matcher; import org.hamcrest.Matcher;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Rule;
import org.junit.rules.TestRule; import org.junit.rules.TestRule;
import org.junit.runner.Description; import org.junit.runner.Description;
import org.junit.runners.model.Statement; import org.junit.runners.model.Statement;
...@@ -29,12 +30,28 @@ import static org.hamcrest.Matchers.allOf; ...@@ -29,12 +30,28 @@ import static org.hamcrest.Matchers.allOf;
/** /**
* JUnit {@code @Rule} to capture output from System.out and System.err. * JUnit {@code @Rule} to capture output from System.out and System.err.
* <p>
* To use add as a {@link Rule @Rule}:
*
* <pre class="code">
* public class MyTest {
*
* &#064;Rule
* public OutputCaptureRule output = new OutputCaptureRule();
*
* &#064;Test
* public void test() {
* assertThat(output).contains("ok");
* }
*
* }
* </pre>
* *
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson * @author Andy Wilkinson
* @since 2.2.0 * @since 2.2.0
*/ */
public class OutputCaptureRule implements TestRule { public class OutputCaptureRule implements TestRule, CapturedOutput {
private final OutputCapture delegate = new OutputCapture(); private final OutputCapture delegate = new OutputCapture();
...@@ -73,6 +90,21 @@ public class OutputCaptureRule implements TestRule { ...@@ -73,6 +90,21 @@ public class OutputCaptureRule implements TestRule {
OutputCaptureRule.this.delegate.reset(); 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 @Override
public String toString() { public String toString() {
return this.delegate.toString(); return this.delegate.toString();
......
...@@ -29,12 +29,18 @@ import static org.assertj.core.api.Assertions.assertThat; ...@@ -29,12 +29,18 @@ import static org.assertj.core.api.Assertions.assertThat;
public class OutputCaptureRuleTests { public class OutputCaptureRuleTests {
@Rule @Rule
public OutputCaptureRule outputCapture = new OutputCaptureRule(); public OutputCaptureRule output = new OutputCaptureRule();
@Test @Test
public void toStringShouldReturnAllCapturedOutput() { public void toStringShouldReturnAllCapturedOutput() {
System.out.println("Hello World"); 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");
} }
} }
...@@ -34,7 +34,7 @@ import static org.hamcrest.Matchers.allOf; ...@@ -34,7 +34,7 @@ import static org.hamcrest.Matchers.allOf;
* @author Andy Wilkinson * @author Andy Wilkinson
* @since 2.2.0 * @since 2.2.0
*/ */
public class OutputCaptureRule implements TestRule { public class OutputCaptureRule implements TestRule, CapturedOutput {
private final OutputCapture delegate = new OutputCapture(); private final OutputCapture delegate = new OutputCapture();
...@@ -64,6 +64,21 @@ public class OutputCaptureRule implements TestRule { ...@@ -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 @Override
public String toString() { public String toString() {
return this.delegate.toString(); return this.delegate.toString();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment