I am writing code for a programming contest in java. The input to the program is set using stdin, and the output to stdout. How do you people test programs that run on stdin / stdout? That's what I think:
Since System.in is of type InputStream and System.out is of type PrintStream, I wrote my code in func with this prototype:
void printAverage(InputStream in, PrintStream out)
Now I would like to test this with junit. I would like to fake System.in with String and get the output in String.
@Test void testPrintAverage() { String input="10 20 30"; String expectedOutput="20"; InputStream in = getInputStreamFromString(input); PrintStream out = getPrintStreamForString(); printAverage(in, out); assertEquals(expectedOutput, out.toString()); }
What is the βrightβ way to implement getInputStreamFromString () and getPrintStreamForString ()?
Am I making it more complicated than it should be?
java junit mocking
user674669 Nov 11 '12 at 7:08 2012-11-11 07:08
source share