Skip to content

Instantly share code, notes, and snippets.

@davidglavas
Created June 4, 2017 17:59
Show Gist options
  • Save davidglavas/7532bfa0ac84de2e1c1dbb90a4d913f0 to your computer and use it in GitHub Desktop.
Save davidglavas/7532bfa0ac84de2e1c1dbb90a4d913f0 to your computer and use it in GitHub Desktop.
TestCase
package week_5;
import static org.junit.Assert.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class Problem_X_Test {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
private final InputStream originalIn = System.in;
private final PrintStream originalOut = System.out;
private final PrintStream originalErr = System.err;
@Before
public void setUpStreams() {
System.setOut(new PrintStream(outContent));
System.setErr(new PrintStream(errContent));
}
@After
public void cleanUpStreams() {
System.setIn(originalIn);
System.setOut(originalOut);
System.setErr(originalErr);
}
@Test
public void sampleInput1() throws NumberFormatException, IOException {
// specify input here
String sampleInput = String.join(System.getProperty("line.separator"),
"2",
"2",
"3 4",
"5 6",
"7 8",
"",
"9",
"10 11 12",
"13 14 15"
);
// redirect stdIn
System.setIn(new ByteArrayInputStream(sampleInput.getBytes()));
// specify expected output
String expectedOut = "Case #1: \nCase #2: \nCase #3: \nCase #4: \nCase #5: \n";
// specify expected output
String expectedOut2 = String.join(System.getProperty("line.separator"),
"Case #1: yes",
"Case #2: no",
""
);
// call some method which will read from stdin and output to stdout
Problem_X.main(null);
// stores actual output
String actualOut = outContent.toString();
// compares expected and actual output
assertEquals(expectedOut, actualOut);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment