官术网_书友最值得收藏!

Test – board boundaries I

We should start by checking whether a piece is placed within the boundaries of the 3x3 board:

package com.packtpublishing.tddjava.ch03tictactoe;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class TicTacToeSpec {
  @Rule
  public ExpectedException exception = ExpectedException.none();
  private TicTacToe ticTacToe;

  @Before
  public final void before() {
    ticTacToe = new TicTacToe();
  }
@Test public void whenXOutsideBoardThenRuntimeException() { exception.expect(RuntimeException.class); ticTacToe.play(5, 2); } }

When a piece is placed anywhere outside the x-axis, then RuntimeException is thrown.

In this test, we are defining that RuntimeException is expected when the ticTacToe.play(5, 2) method is invoked. It's a very short and easy test, and making it pass should be easy as well. All we have to do is create the play method and make sure that it throws RuntimeException when the x argument is smaller than 1 or bigger than 3 (the board is 3x3). You should run this test three times. The first time, it should fail because the play method doesn't exist. Once it is added, it should fail because RuntimeException is not thrown. The third time, it should be successful because the code that corresponds with this test is fully implemented.

主站蜘蛛池模板: 靖安县| 会理县| 揭东县| 旅游| 阳东县| 巨野县| 永善县| 新乡市| 桂林市| 芜湖市| 西安市| 芜湖市| 云霄县| 易门县| 瓦房店市| 纳雍县| 杭锦旗| 信宜市| 绿春县| 乐东| 泉州市| 大悟县| 丰都县| 繁峙县| 山西省| 栾城县| 微博| 榆中县| 南宁市| 商河县| 广昌县| 霍城县| 台南县| 江陵县| 通榆县| 盐津县| 康平县| 兰州市| 三穗县| 茂名市| 乌鲁木齐市|