我想測試下面的兩種方法,但因爲它基於隨機輸出我的轉到assertEquals()
將無法正常工作。如何JUnit測試在Java中的任何輸出
我只是想測試,以確保方法正在產生某種輸出。有任何想法嗎?
新手程序員,感謝幫助。
public void compRandomChoice() {
double choice = (Math.random() * 100/100);
if (choice > 0 && choice <= 0.34) {
computer.setChoice(HandThrow.ROCK);
} else if (choice > 0.34 && choice <= 0.67) {
computer.setChoice(HandThrow.PAPER);
} else {
computer.setChoice(HandThrow.SCISSORS);
}
}
public String gameWinner() {
String gameResult;
if (human.getChoice() == computer.getChoice()) {
gameResult = "ITS A TIE!";
} else if (human.getChoice() == HandThrow.ROCK
&& computer.getChoice() == HandThrow.SCISSORS
|| human.getChoice() == HandThrow.PAPER
&& computer.getChoice() == HandThrow.ROCK
|| human.getChoice() == HandThrow.SCISSORS
&& computer.getChoice() == HandThrow.PAPER) {
gameResult = "CONGRATS, YOU WIN!";
} else {
gameResult = "COMPUTER WINS!";
}
return gameResult;
}
這個貌似給出的選擇最佳路線,但是這需要我做周圍的MVC概念基於我的代碼一些廣泛的重組,所以現在我使用的是assertNotNull,把工作做在緊要關頭。謝謝! – phamousphil 2013-05-14 03:23:58