我是TDD新手,遇到問題。據我所知,在生產代碼準備就緒之前,TDD中的所有測試都應該失敗。我已經開發了下面的代碼:TDD:如何創建測試初始值的測試
汽車接口:
public interface CarOperations{
/**
* Return current car speed in km/h
*/
public int currentSpeed();
}
汽車類:
public class Car implements CarOperations{
@Override
public int currentSpeed() {
// TODO Auto-generated method stub
return 0;
}}
JUnit測試:
@Test
public void testInitSpeed() {
Car car = new Car();
assertEquals(0, car.currentSpeed());
}
因此,我希望這個測試將失敗,因爲在我開發任何生產代碼之前,每個TDD測試都應該失敗,但是由於int默認值是這樣的測試會通過。我可以將方法的返回類型更改爲Integer並接收NULL值,但是我想知道在這種情況下如何處理基元?也許我的測試是錯誤的?
相關:http://programmers.stackexchange.com/questions/224365/in-tdd-is-it-bad-practice-to-pass-a-test-with-code-that-would-pass-more - 對 - 在我看來,這樣一個小事件不是問題 – assylias