我有一些方法,诸如简单的数组类Add(int n),AddAt(int n, int index)等.
addAt方法从超类调用另一个方法
public void addAt(int n, int index) {
if (checkIndex(index, size + 1)){
...
}
}
Run Code Online (Sandbox Code Playgroud)
检查插入的索引是否不受限制,如果是,超类方法会向控制台输出错误消息.
如果邮件被打印,我应该如何测试?我正在使用JUnit4.12-beta
@Test
public void testPrint() {
ByteArrayOutputStream outContent = new ByteArrayOutputStream();
//redirect the System-output (normaly the console) to a variable
System.setErr(new PrintStream(outContent));
//call your method here
//check if your error message is in the output variable
assertEquals("your output", outContent.toString());
}
Run Code Online (Sandbox Code Playgroud)