输出:
final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
System.setOut(new PrintStream(outContent));
两行代码将控制台输出截取到outContent中,通过outContent.toString()即可调用控制台输出
随后进行断言即可。
内容来自:https://blog.csdn.net/april0012/article/details/45307355
输入:
String data = "Hello, World!\r\n";
InputStream stdin = System.in;
try {
System.setIn(new ByteArrayInputStream(data.getBytes()));
Scanner scanner = new Scanner(System.in);
System.out.println(scanner.nextLine());
} finally {
System.setIn(stdin);
}
如上述代码,将data加入输入流中即可,scanner类将会读入data作为控制台输入
内容来自:https://stackoverflow.com/questions/1647907/junit-how-to-simulate-system-in-testing