我從控制檯掃描輸入的方法:Java單元測試輸入例外
private int chooseItem() throws IOException {
return inputoutput.inputValue();
}
public int inputValue() throws IOException {
Scanner scanner = new Scanner(System.in);
return scanner.nextInt();
}
正如你可以看到它期望得到int
爲傳入值,如果我進入String
,它應該拋出InputMismatchEcxeption
。問題是如何在Java中,我可以教inputValue返回smth不int,例如,字符串和檢查異常被拋出的事實?換句話說要測試它? 我想:
IO io = mock(IO.class);
when(io.inputValue()).thenReturn("fdas");
但剛剛的Mockito說,io.inputValue不能返回字符串。
這不是OP所要求的。 –
我知道))))問題是我不能真正給字符串inputValue,因爲我無法通過控制檯測試它。我需要somethow命令inputValue返回不是int,但有一些錯誤來測試異常。 – ovod
啊我看到了...然後我認爲你可能不得不改變輸入流從其他流中讀取。 – manouti