I am currently trying to Junit test a game for a Uni assignment. Below is an example of the kind of methods I am trying to test.
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;
}
I am using the following test class to test this:
/**
* @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");
}
Every time I try and run this test only the first test will run and the scanner is not re-prompted for the subsequent 2. Is there any way around this? I would greatly appreciate any advice concerning this issue or for a better/more efficient way to test the above method .