Sum { static void sum(InputStream in, PrintStream out) { try ( Scanner scanner = new Scanner(in);) { int a = scanner.nextInt(); int b = scanner.nextInt(); int s = a + b; out.print(s); } }  static void main(String[] args);  }
@Test public void test() throws Exception { PipedOutputStream pos = new PipedOutputStream(); InputStream in = new PipedInputStream(pos); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrintStream out = new PrintStream(baos); pos.write("3\n4\n".getBytes()); Sum.sum(in, out); String output = new String(baos.toByteArray()); assertEquals("7", output); }