- Test-Driven Java Development(Second Edition)
- Alex Garcia Viktor Farcic
- 195字
- 2021-06-24 18:31:52
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.
推薦閱讀
- WildFly:New Features
- Mastering OpenCV 4
- Building Mapping Applications with QGIS
- SSM輕量級框架應用實戰
- Hands-On Swift 5 Microservices Development
- Yocto for Raspberry Pi
- 自然語言處理Python進階
- Learning Zurb Foundation
- 焊接機器人系統操作、編程與維護
- Android應用案例開發大全(第二版)
- Web App Testing Using Knockout.JS
- C++ Data Structures and Algorithm Design Principles
- 微信公眾平臺服務號開發:揭秘九大高級接口
- RabbitMQ Essentials
- Citrix XenDesktop? Cookbook(Third Edition)