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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

一种编写测试的好方法

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

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

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

CustomerService是我們的界面。 為了使示例保持簡單,它有兩種方法,下面將進(jìn)行介紹。 注意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; }

從圖中可以看到,我們有兩個(gè)此類的實(shí)現(xiàn),RemoteCustomerService和CachingCustomerService。 這些的實(shí)現(xiàn)沒有顯示,因?yàn)樗鼈儫o關(guān)緊要。 我怎么說呢 很簡單–我們正在測試合同。 我們?yōu)榻涌谥械拿總€(gè)方法以及合同的每個(gè)排列編寫測試。 例如,對于get(),我們需要測試存在具有給定用戶名的客戶時(shí)發(fā)生的情況,以及不存在時(shí)發(fā)生的情況。

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(); }

現(xiàn)在,我們已經(jīng)對合同進(jìn)行了測試,并且在任何時(shí)候都沒有提到任何實(shí)現(xiàn)。 這意味著兩件事:

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

就是這樣! 現(xiàn)在,我們有了一種非常簡單的方法來測試任何接口的多個(gè)實(shí)現(xiàn),方法是預(yù)先進(jìn)行艱苦的工作,并將測試新實(shí)現(xiàn)的工作減少到一個(gè)簡單的方法中。

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

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

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。