org.junit.contrib.java.lang.system.DisallowWriteToSystemOut Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of system-rules Show documentation
Show all versions of system-rules Show documentation
A collection of JUnit rules for testing code which uses java.lang.System.
package org.junit.contrib.java.lang.system;
import org.junit.contrib.java.lang.system.internal.DisallowWrite;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import static org.junit.contrib.java.lang.system.internal.PrintStreamHandler.SYSTEM_OUT;
/**
* {@code DisallowWriteToSystemOut} lets a test fail if it tries to write
* something to {@code System.out}.
*
* For that purpose you only have to add {@code DisallowWriteToSystemOut}
* rule to your test class
*
* public class TestClass {
* @Rule
* public final DisallowWriteToSystemOut disallowWriteToSystemOut
* = new DisallowWriteToSystemOut();
*
* @Test
* public void this_test_fails() {
* System.out.println("some text");
* }
* }
*
*
* @see DisallowWriteToSystemErr
* @since 1.14.0
*/
public class DisallowWriteToSystemOut implements TestRule {
private final DisallowWrite disallowWrite = new DisallowWrite(SYSTEM_OUT);
public Statement apply(final Statement base, Description description) {
return disallowWrite.createStatement(base);
}
}