Merge pull request #16948 from izeye

* pr/16948:
  Polish
pull/16972/head
Stephane Nicoll 6 years ago
commit 1c14079246

@ -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<Matcher<? super String>> 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);

@ -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 {

@ -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!!!");
}
}

Loading…
Cancel
Save