2015-09-03 251 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

你能告訴我該怎麼做嗎? –