日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

一种编写测试的好方法

發布時間:2023/12/3 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 一种编写测试的好方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

測試。 最近我一直在考慮進行測試。 作為我對各種項目所做的代碼審查的一部分,我已經看到了數千行未經測試的代碼。 這不僅是測試覆蓋率統計數據指出這一點的情況,還更多是該項目中根本沒有任何測試的情況 。 我一直聽到這種悲慘狀況的兩個原因? “我們沒有時間”,緊隨其后的是“完成代碼后就去做”。

我在這里展示的不是萬能藥。 它涵蓋了單元測試,尤其是接口的單元測試。 接口是好東西。 接口定義合同。 接口,無論接口有多少種實現方式,都可以輕松,輕松地進行測試。 讓我們看看如何使用此類結構作為示例。

CustomerService是我們的界面。 為了使示例保持簡單,它有兩種方法,下面將進行介紹。 注意javadoc-這是描述合同的地方。

public interface CustomerService {/*** Retrieve the customer from somewhere.* @param userName the userName of the customer* @return a non-null Customer instance compliant with the userName* @throws CustomerNotFoundException if a customer with the given user name can not be found*/Customer get(String userName) throws CustomerNotFoundException;/*** Persist the customer.* @param customer the customer to persist* @return the customer as it now exists in its persisted form* @throws DuplicateCustomerException if a customer with the user name already exists*/Customer create(Customer customer) throws DuplicateCustomerException; }

從圖中可以看到,我們有兩個此類的實現,RemoteCustomerService和CachingCustomerService。 這些的實現沒有顯示,因為它們無關緊要。 我怎么說呢 很簡單–我們正在測試合同。 我們為接口中的每個方法以及合同的每個排列編寫測試。 例如,對于get(),我們需要測試存在具有給定用戶名的客戶時發生的情況,以及不存在時發生的情況。

public abstract class CustomerServiceTest {@Testpublic void testCreate(){CustomerService customerService = getCustomerService();Customer customer = customerService.create(new Customer("userNameA"));Assert.assertNotNull(customer);Assert.assertEquals("userNameA",customer.getUserName());}@Test(expected = DuplicateCustomerException.class)public void testCreate_duplicate(){CustomerService customerService = getCustomerService();Customer customer = new Customer("userNameA");customerService.create(customer);customerService.create(customer);}@Testpublic void testGet(){CustomerService customerService = getCustomerService();customerService.create(new Customer("userNameA"));Customer customer = customerService.get("userNameA");Assert.assertNotNull(customer);Assert.assertEquals("userNameA",result.getUserName());}@Test(expected = CustomerNotFoundException.class)public void testGet_noUser(){CustomerService customerService = getCustomerService();customerService.get("userNameA");}public abstract CustomerService getCustomerService(); }

現在,我們已經對合同進行了測試,并且在任何時候都沒有提到任何實現。 這意味著兩件事:

  • 我們不需要為每個實現重復測試。 這是一件非常好的事情。
  • 沒有一個實現正在測試中。 我們可以通過為每個實現添加一個測試類來糾正此問題。 由于每個測試類幾乎都是相同的,因此我將僅演示RemoteCustomerService的測試。
public class RemoteCustomerServiceTest extends CustomerServiceTest {public CustomerService getCustomerService(){return new RemoteCustomerService();} }

就是這樣! 現在,我們有了一種非常簡單的方法來測試任何接口的多個實現,方法是預先進行艱苦的工作,并將測試新實現的工作減少到一個簡單的方法中。

參考: Objective博客上的JCG合作伙伴 Steve Chaloner 編寫測試的一種好方法 。

翻譯自: https://www.javacodegeeks.com/2013/06/a-good-lazy-way-to-write-tests.html

總結

以上是生活随笔為你收集整理的一种编写测试的好方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。