如果返回null,则测试Tennis Kata Java失败

时间:2018-06-10 02:05:09

标签: java junit tdd

我为TDD找到了网球卡塔训练,但我对If声明有一点小问题。

public String getScore(){
    if (server.getScore() >= 3 && receiver.getScore() >= 3){
        if(Math.abs(receiver.getScore() - server.getScore()) >= 2){
            return getLeadPlayer().getName() + " won";
        } else if (Math.abs(server.getScore() - receiver.getScore()) >= 1) {
            return "A" + ":" + receiver.getScoreDescription();
        } else if (Math.abs(receiver.getScore() - server.getScore()) >= 1) {
            return server.getScoreDescription() + ":" + "A";
        } else {
            return "40:40";
        }
    } else {
        return server.getScoreDescription() + ":" + receiver.getScoreDescription();
    }

第二个if语句返回null。我不知道出了什么问题。

@Test
public void receiverAdvantage() {
    IntStream.rangeClosed(1, 3).forEach((Integer) -> {
        server.winBall();
    });
    IntStream.rangeClosed(1, 4).forEach((Integer) -> {
        receiver.winBall();
    });
    assertThat(tennisGame, hasProperty("score", is("40:A")));
}

“A”意味着优势。

测试结果:

java.lang.AssertionError:
Expected: hasProperty("score", is "40:A")
 but: null

0 个答案:

没有答案