我在junit 5中编写了一个测试,测试了终端中的用户输入,但出现错误“ java.util.NoSuchElementException”。
我想测试终端中的用户输入。
//GIVEN
System.setIn(new ByteArrayInputStream("5\n1\n1\n1\n1\n1\n1\n1\n1\n1\n".getBytes()));
createPlayer();
GameScore gameScore = new GameScore(players);
gameScore.score();
//WHEN
String[] output = outContent.toString().replace("\r\n", "\n").split("\n");
//THEN
assertEquals("Veuillez choisir entre l'attaque basic ou special (1-2)", output[2]);
public int intReader(int min, int max, String errorMessage){
int inputRead = -1;
do{
try{
inputRead = sc.nextInt();
responseIsGood = (inputRead >= min && inputRead <= max);
}catch (InputMismatchException e){
sc.next();
responseIsGood = false;
}
if (!responseIsGood) System.out.println(errorMessage);
}while (!responseIsGood);
return inputRead;
}
java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at com.magicworld.interaction.InputReader.intReader(InputReader.java:21)
at com.magicworld.interaction.GameScore.score(GameScore.java:36)