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

Requirement 2 – adding two-player support

Now it's time to work on the specification of which player is about to play his turn.


There should be a way to find out which player should play next.

We can split this requirement into three tests:

  • The first turn should be played by player X
  • If the last turn was played by X, then the next turn should be played by O
  • If the last turn was played by O, then the next turn should be played by X

Until this moment, we haven't used any of the JUnit's asserts. To use them, we need to import the static methods from the org.junit.Assert class:

import static org.junit.Assert.*;

In their essence, methods inside the Assert class are very simple. Most of them start with assert. For example, assertEquals compares two objects—assertNotEquals verifies that two objects are not the same and assertArrayEquals verifies that two arrays are the same. Each of those asserts has many overloaded variations so that almost any type of Java object can be used.

In our case, we'll need to compare two characters. The first is the one we're expecting and the second one is the actual character retrieved from the nextPlayer method.

Now it's time to write those tests and the implementation.


Write the test before writing the implementation code .

The benefits of doing this are as follows—it ensures that testable code is written and ensures that every line of code gets tests written for it.

By writing or modifying the test first, the developer is focused on requirements before starting to work on a code. This is the main difference when compared to writing tests after the implementation is done. An additional benefit is that with tests first, we are avoiding the danger that the tests work as quality checking instead of quality assurance.
主站蜘蛛池模板: 固镇县| 施秉县| 拜城县| 绥阳县| 鹤岗市| 乐平市| 陆川县| 昌都县| 阿拉善右旗| 元朗区| 东丰县| 富裕县| 屏南县| 江川县| 武山县| 穆棱市| 永福县| 徐闻县| 云霄县| 临邑县| 五大连池市| 灵石县| 新河县| 台南县| 麦盖提县| 河曲县| 工布江达县| 普兰县| 镇康县| 兴宁市| 根河市| 松潘县| 泉州市| 翼城县| 茶陵县| 兰溪市| 安丘市| 金秀| 仁化县| 阿城市| 隆回县|