2015-09-03 202 views
-1

我有一个Hangman游戏应用程序的项目,它从控制台输入中读取猜测并对其进行单元测试,但我不知道如何测试它。他们告诉我在最后一种方法中使它返回真或假,因为我的代码是无法测试的,这会帮助我测试它,就像我玩游戏/赢得并失去/。但我不知道该怎么做。有人可以帮助我测试它吗?Hangman JUnit测试

这里是我的代码:

private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); 
private static Scanner scanner = new Scanner(System.in); 

private String secredWord; 
private StringBuilder dashes; 
private int lives; 
private int guesses; 
private char[] wrongGuess; 

/** 
* Generates a string with all letters in the searched word replaced with an 
* underline and a space. 
* 
* @return return the secred word hidden. 
*/ 
public StringBuilder makeDashes() { 
    dashes = new StringBuilder(); 

    for (int i = 0; i < secredWord.length(); i++) { 
     dashes.append("_ "); 

    } 
    return dashes; 
} 

/** 
* Takes a string as a parameter and set the sacred word to be guessed, and 
* reset the lives & guesses that are made. 
* 
* @param secredWord 
*   the word that has to be guessed. 
* 
* @return - returns the method that make the word hidde. 
*/ 
public StringBuilder setGame(String secredWord) { 
    this.secredWord = secredWord; 
    lives = 0; 
    guesses = 0; 
    wrongGuess = new char[6]; 

    return makeDashes(); 
} 

/** 
* Make a check for a repeatable guesses. 
* 
* @param guess 
*   Characters to check 
* @return - returns true or false. If true it make you guess another 
*   character. 
*/ 
private boolean repeatGuesses(char guess) { 
    for (int i = 0; i < dashes.length(); i = i + 2) { 
     if (dashes.charAt(i) == guess) { 
      return true; 

     } 

    } 

    for (int i = 0; i < wrongGuess.length; i++) { 
     if (wrongGuess[i] == guess) { 
      return true; 

     } 
    } 
    return false; 

} 

/** 
* Reads a letter and check if its been guessed before. 
*/ 
private void guessLetter() { 
    char guess; 
    boolean present = false; 

    LOGGER.info("Make a guess: "); 
    guess = scanner.next().charAt(0); 

    while (repeatGuesses(guess)) { 
     LOGGER.info("The letter has been guessed already!"); 
     LOGGER.info("Make another guess: "); 
     guess = scanner.next().charAt(0); 

    } 

    for (int i = 0; i < secredWord.length(); i++) { 
     if (secredWord.charAt(i) == guess) { 
      dashes.setCharAt(i * 2, guess); 
      present = true; 
      guesses++; 

     } 

    } 
    if (!present) { 
     LOGGER.info("Wrong guess"); 
     wrongGuess[lives++] = guess; 

    } 
} 

/** 
* While you didnt guess the word or you have more lifes left calls the 
* motod guessLetter() and you have to make a guess untill you lost your 
* lifes or guess the word. 
*/ 
public void playTheGame() { 
    while (lives < 6 && guesses < secredWord.length()) { 
     LOGGER.info("Hidden word --> {}", dashes); 
     LOGGER.info("Lives: ({}/6 wrong letters)", lives); 
     guessLetter(); 

    } 
    if (lives == 6) { 
     LOGGER.info("You lost!The word was --> {}", secredWord); 
    } else { 
     LOGGER.info("Congratulations YOU WON!!! The word was --> {}.", secredWord); 

    } 
} 

并作出一个测试,看看是隐藏'这个词

public void hiddenWordTest() { 
    HangmanGame item = new HangmanGame(); 

    String secredWord = "hangman"; 

    String correctHidden = "_ _ _ _ _ _ _ "; 
    StringBuilder resultHidden = item.setGame(secredWord); 
    assertEquals(correctHidden, resultHidden.toString()); 

}` 
+0

您应该提取与游戏逻辑相关的核心方法,然后仅测试那些返回给定输入的正确值的方法。 – mic4ael

+0

如何做到这一点? –

回答

0

我能想到的两个选项:

  1. 可以传递在LOGGER中通过构造器在测试过程中伪造
  2. 提取计算逻辑分成单独的课程并单独进行测试,无需任何假货。
+0

你能告诉我该怎么做吗? –