mardi 26 mai 2015

JUnit testing: simulating user input

I need to test a method that asks the user for an input and charges the player the inputted amount. The method to be tested:

public void askForBetSize() {
    System.out.println("\nYour stack: " + player.getBalance());
    System.out.print("Place your bet: ");
    bet = Integer.parseInt(keyboard.nextLine()); // = this needs to be simulated
    player.charge(bet);
}

Current unit test is:

@Test 
public void bettingChargesPlayerRight() {
    round.setCards();
    round.askForBetSize(); // here I would like to simulate a bet size of 100
    assertEquals(900, round.getPlayer().getBalance()); // default balance is 1000
}

I tried to implement this and this but after testing previous classes the test stopped running when it started to test this method.

Aucun commentaire:

Enregistrer un commentaire