Allow matchers to be used to verify output captured by OutputCapture

Closes gh-3330
pull/3333/head
Andy Wilkinson 10 years ago
parent 0ec9de9137
commit 31336c2dce

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -20,17 +20,24 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.PrintStream; import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import org.hamcrest.Matcher;
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;
import org.springframework.boot.ansi.AnsiOutput; import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.ansi.AnsiOutput.Enabled; import org.springframework.boot.ansi.AnsiOutput.Enabled;
import static org.hamcrest.Matchers.allOf;
import static org.junit.Assert.assertThat;
/** /**
* JUnit {@code @Rule} to capture output from System.out and System.err. * JUnit {@code @Rule} to capture output from System.out and System.err.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson
*/ */
public class OutputCapture implements TestRule { public class OutputCapture implements TestRule {
@ -40,6 +47,8 @@ public class OutputCapture implements TestRule {
private ByteArrayOutputStream copy; private ByteArrayOutputStream copy;
private List<Matcher<? super String>> matchers = new ArrayList<Matcher<? super String>>();
@Override @Override
public Statement apply(final Statement base, Description description) { public Statement apply(final Statement base, Description description) {
return new Statement() { return new Statement() {
@ -49,10 +58,18 @@ public class OutputCapture implements TestRule {
try { try {
base.evaluate(); base.evaluate();
} }
finally {
try {
if (!OutputCapture.this.matchers.isEmpty()) {
String output = OutputCapture.this.toString();
assertThat(output, allOf(OutputCapture.this.matchers));
}
}
finally { finally {
releaseOutput(); releaseOutput();
} }
} }
}
}; };
} }
@ -88,6 +105,16 @@ public class OutputCapture implements TestRule {
return this.copy.toString(); return this.copy.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<String> matcher) {
this.matchers.add(matcher);
}
private static class CaptureOutputStream extends OutputStream { private static class CaptureOutputStream extends OutputStream {
private final PrintStream original; private final PrintStream original;

Loading…
Cancel
Save