我目前正在尝试对 Uni 作业的游戏进行 Junit 测试。下面是我正在尝试测试的方法的示例。
public static int choosePlayers(int num) {
while (validPlayerNumber == false) {
try {
System.out.print("Please enter Number of players (2-4)\n> ");
num = in.nextInt();
switch (num) {
case 2:
validPlayerNumber = true;
numberPlayers = num;
System.out.println(numberPlayers + " players selected");
break;
case 3:
validPlayerNumber = true;
numberPlayers = num;
System.out.println(numberPlayers + " players selected");
break;
case 4:
validPlayerNumber = true;
numberPlayers = num;
System.out.println(numberPlayers + " players selected");
break;
default:
throw new IllegalArgumentException("Sorry, that is not a valid selection.");
// System.out.println("Sorry, that is not a valid selection.");
}
} catch (InputMismatchException ex) {
// log the exception
System.out.println("Problem with input : " + ex.toString());
continue;
}
}
return numberPlayers;
}
我正在使用以下测试类来测试它:
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
num1 =1;
num2= 2;
num4 = 4;
num3 = 3;
num5 = 5;
game= new Game();
}
@Test
public void testchoosePlayers2() {
System.out.println("Testing choosingPlayers 2");
Scanner scanner = new Scanner (System.in);
int expected = scanner.nextInt();
int actual = game.choosePlayers(num2);
assertEquals(expected, actual);
System.out.println("Test finsihed");
}
@Test
public void testchoosePlayers3() {
System.out.println("Testing choosingPlayers 3");
Scanner scanner = new Scanner (System.in);
int expected = scanner.nextInt();
int actual = game.choosePlayers(num3);
assertEquals(expected, actual);
System.out.println("Test finsihed");
}
@Test
public void testchoosePlayers4() {
System.out.println("Testing choosingPlayers 4");
Scanner scanner = new Scanner (System.in);
int expected = scanner.nextInt();
int actual = game.choosePlayers(num4);
assertEquals(expected, actual);
System.out.println("Test finsihed");
}
每次我尝试运行此测试时,只会运行第一个测试,并且不会重新提示扫描仪进行后续的 2 次测试。有什么办法可以解决此问题吗?我非常感谢有关此问题的任何建议或更好/更有效的方法来测试上述方法。
最佳答案
您的目的是测试该方法使用提供的输入参数返回正确的值。如果扫描的值不正确,测试将失败。因此,不必扫描期望值,只需将期望值放入断言中即可。此外,您不需要在这里进行单独的测试,因为您测试的是相同的功能。
要模拟输入,您需要将 System.in 替换为您自己的输入流。请参阅JUnit: How to simulate System.in testing?了解详情。
private final InputStream systemIn = System.in;
private ByteArrayInputStream testIn;
@After
public void resetSystemIn() {
System.setIn(systemIn);
}
private void inputData(String data) {
testIn = new ByteArrayInputStream(data.getBytes());
System.setIn(testIn);
}
@Test
public void testchoosePlayers() {
System.out.println("Testing choosingPlayers 2");
inputData("2");
int actual = game.choosePlayers(num2);
assertEquals(num2, actual);
System.out.println("Testing choosingPlayers 3");
inputData("3");
actual = game.choosePlayers(num3);
assertEquals(num3, actual);
System.out.println("Testing choosingPlayers 4");
inputData("4");
actual = game.choosePlayers(num4);
assertEquals(num4, actual);
System.out.println("Test finsihed");
}
关于java - Junit测试扫描仪输入问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55068303/