我正在關注敏捷實踐課程,並且我有作業。在課程中,我已經教過如何使用FIT進行自動驗收測試。從我所能看到的,FIT似乎正在消失,我不再確定這是我想用於作業的技術。無論如何...接受測試遺留代碼
我有遺留系統測試,一切都設計不良/編碼。當然,沒有測試,在寫任何驗收測試之前我不允許重構或改變功能。
爲了簡單起見,我們假設系統是一個商店收據系統,它將產品列表(含價格)和總成本列印到標準輸出流。事情是這樣的:
public class ReceiptSystem
{
List<Product> boughtProducts = new ArrayList<Product>();
public void buyProduct(Product p)
{
boughtProducts.add(p);
}
public void printReceipt()
{
List<ReceiptLine> receipt = new ArrayList<ReceiptLine>();
int total = 0;
int price;
for(List<Product> boughtProducts : boughtProduct)
{
price = SomeThirdPartyClass.calculatePrice(boughtProduct.getBarcode());
System.out.println(boughtProduct + ": " + price);
total += price;
}
System.out.println("TOTAL: " + total);
}
}
的問題是,價格是由第三方系統與外部庫計算的,所以我不知道他們,我很難(如果不是不可能的)寫驗收使用FIT進行測試。在正常情況下,我會做這樣的事情:
購物清單(輸入):
-------------------
| product's barcode |
-------------------
| 111111111 |
-------------------
| 123456789 |
-------------------
| 987654321 |
-------------------
| 999999999 |
-------------------
收據(預期輸出):
------------------
| receipt |
------------------
| 111111111: 1.99 |
------------------
| 123456789: 2.99 |
------------------
| 987654321: 3.99 |
------------------
| 999999999: 4.99 |
------------------
| TOTAL: 13.96 |
------------------
但我怎麼能做到這一點不知道價格?採用什麼最好的策略?有沒有其他的框架來實現這一目標?
謝謝。
更新:的SomeThirdPartyClass
是final
,試圖嘲笑,或寫一個代理類,它會給以下錯誤:
Cannot subclass final class class SomeThirdPartyClass
而且,它是不可變和公共構造函數沒有參數。好像老師講的目的....
謝謝,@GarrettHall。確切地說,這就是我在真實情況下所要做的。但是我不允許在編寫任何代碼之前以任何方式更改代碼(這是作業要求的一部分...) – satoshi 2012-02-27 18:34:12
然後只捕獲輸出,然後從中創建迴歸測試。 FITnesse是您可能想要查看的FIT的更新版本,但JUnit應該可以正常工作。 – 2012-02-27 18:44:03
我同意@GarrettHall。您可以使用模擬框架,如[Mockito](http://code.google.com/p/mockito/)模擬來自第三方庫的響應。例如。 'Mockito.when(SomeThirdPartyLibrary.calculatePrice( 「11111111」))。thenReturn(「1。99「);' – radimpe 2012-02-27 19:00:55