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

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.

主站蜘蛛池模板: 五台县| 梓潼县| 栖霞市| 来宾市| 中牟县| 姜堰市| 景泰县| 麻江县| 色达县| 沭阳县| 东乡县| 武乡县| 涞水县| 宁陕县| 阳曲县| 阳信县| 嵊州市| 札达县| 砀山县| 江油市| 自贡市| 察隅县| 临武县| 深泽县| 迁西县| 万山特区| 华安县| 龙井市| 浮梁县| 高唐县| 奉节县| 宝兴县| 昭苏县| 普洱| 泰顺县| 南通市| 浑源县| 永善县| 兰考县| 黑河市| 朔州市|