14. import static org.junit.Assert.*;
import org.junit.Test; まだなし
public class GoodsTest {
@Test
public void testGetGoodsPrice() {
Goods ice = new Goods(100);
assertEquals (100, ice.getPrice());
}
}
Goodsクラスがまだ定義されていないのでテストを実行すると失敗(赤)
15. import static org.junit.Assert.*; public class Goods {
import org.junit.Test;
public class GoodsTest { public Goods(int i) {
@Test }
public void testGetGoodsPrice() {
Goods ice = new Goods(100); public int getPrice() {
assertEquals(100, ice.getPrice()); return 100;
} }
}
}
Eclipseのクイック?フィックスでGoodsクラスを自動生成、少し修正
16. import static org.junit.Assert.*; public class Goods {
import org.junit.Test; private int unitPrice = 0;
public class GoodsTest { public Goods(int unitPrice) {
@Test this.unitPrice = unitPrice;
public void testGetGoodsPrice() { }
Goods ice = new Goods(100); public int getPrice() {
assertEquals(100, ice.getPrice()); return this.unitPrice;
} }
} }
変数名を変更、クラス変数を作成→テストを実行し、問題の無いことを確認
17. import static org.junit.Assert.*; public class Goods {
import org.junit.Test; private int unitPrice = 0;
public class GoodsTest { public Goods(int unitPrice) {
@Test this.unitPrice = unitPrice;
public void testGetGoodsPrice() { }
Goods ice = new public int getPrice() {
Goods(100); return this.unitPrice;
}
assertEquals(100, ice.getPrice());
}
}
@Test
public void testGetTaxIncludedPrice(){
Goods ice = new
Goods(100);
assertEquals(105, ice.getTaxIndludedPr
ice());
}
getTaxIncludedPriceメソッドが未定義で失敗
}
18. import static org.junit.Assert.*; public class Goods {
import org.junit.Test; private int unitPrice = 0;
public class GoodsTest { public Goods(int unitPrice) {
@Test this.unitPrice = unitPrice;
public void testGetGoodsPrice() { }
Goods ice = new public int getPrice() {
Goods(100); return this.unitPrice;
}
assertEquals(100, ice.getPrice());
public int getTaxIndludedPrice() {
}
return 105;
@Test
}
public void testGetTaxIncludedPrice(){
}
Goods ice = new
Goods(100);
assertEquals(105, ice.getTaxIndludedPr
ice());
}
必要最低限の変更でテストをパスする
}
19. import static org.junit.Assert.*; public class Goods {
import org.junit.Test; private int unitPrice = 0;
public class GoodsTest { public Goods(int unitPrice) {
@Test this.unitPrice = unitPrice;
public void testGetGoodsPrice() { }
Goods ice = new public int getPrice() {
Goods(100); return this.unitPrice;
}
assertEquals(100, ice.getPrice());
public int getTaxIndludedPrice() {
}
return 105;
@Test
}
public void testGetTaxIncludedPrice(){
}
Goods ice = new
Goods(100);
Goods juice = new
Goods(500);
assertEquals(105, ice.getTaxIndludedPr
ice());
assertEquals(525, juice.getTaxIndluded
500円のジュースをテストケースに追加。105を返すので失敗。
Price());
}
}
20. import static org.junit.Assert.*; public class Goods {
import org.junit.Test; private int unitPrice = 0;
public class GoodsTest { public Goods(int unitPrice) {
@Test this.unitPrice = unitPrice;
public void testGetGoodsPrice() { }
Goods ice = new public int getPrice() {
Goods(100); return this.unitPrice;
}
assertEquals(100, ice.getPrice());
public int getTaxIndludedPrice() {
}
return
@Test (int)(this.unitPrice*1.05);
public void testGetTaxIncludedPrice(){ }}
Goods ice = new
Goods(100);
Goods juice = new
Goods(500);
assertEquals(105, ice.getTaxIndludedPr
ice());
assertEquals(525, juice.getTaxIndluded
成功。
Price());
}
}