Skip to content

Instantly share code, notes, and snippets.

@ragklaat
Created September 24, 2014 08:29
Show Gist options
  • Save ragklaat/28dad2f3346ae11e6a6b to your computer and use it in GitHub Desktop.
Save ragklaat/28dad2f3346ae11e6a6b to your computer and use it in GitHub Desktop.
JUnit test for methods writing to OutputStream
import static org.junit.Assert.assertEquals;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class SystemOutTest {
private final ByteArrayOutputStream output = new ByteArrayOutputStream();
@Before
public void setUpStreams() {
System.setOut(new PrintStream(output));
}
@After
public void cleanUpStreams() {
System.setOut(null);
}
@Test
public void testSystemOut() {
System.out.print("assertme!");
assertEquals("assertme!", output.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment