我正在编写JUnit测试,我需要模拟像“P”这样的用户输入,然后我希望它能运行一个方法。问题是我目前没有运行该方法的代码,并且在输入错误时不会显示错误。
我的测试代码是,
@Test
public void Test2(){
String data = "J";
boolean error2;
System.setIn(new java.io.ByteArrayInputStream(data.getBytes()));
error2 = WorkshopReviewSystem.GetError();
assertEquals(true, error2);
}
主要代码是,
System.out.println("What do you want to do?\n O = Overview, P = Add Paper, R = Add Review, [num] = Detail of that paper, X = exit");
Scanner in = new Scanner(System.in);
while (in.hasNextLine()){
InLoop = true;
error = false;
String s = in.next();
try{
if (s.equals("O")) {
PrintPaperOverview();
} else if (s.equals("P")){
AddPaper(in);
} else if (s.equals("R")) {
AddReview(in);
} else if (s.equals("X")) {
System.out.println("Goodbye!");
break;
} else if (Integer.parseInt(s) != -1 ) {
PrintAPaper(Integer.parseInt(s)-1);
} else {
System.out.println("Command not recognised");
}
} catch (Exception e) {
error = true;
System.out.println("Something went wrong: " + e.toString() + "\n");
}
唯一的控制台输出是
0
P
如何编写符合我要求的测试?