天天看點

JUnit:Hamcrest的使用

  如果你使用過 eclipse 和 junit4 的話,hamcrest 的使用會讓你如虎添翼。

  1. 下載下傳hamcrest

  下載下傳清單:http://code.google.com/p/hamcrest/downloads/list

  選擇 full hamcrest distribution 版本,完整版。如果是在 linux 下面,可以下載下傳tgz格式的。

  win 下面下載下傳 zip 吧。其他系統的可以根據情況下載下傳。

  2. 解壓縮

  tar   -zxvf   ×××.tgz

  解壓之後,将檔案夾放到合适的目錄下面。假設路徑為 /home/mark/tools/hamcrest-1.2

  3. hamcrest 與 eclipse

  假設你已經建立一個測試項目叫 testhamcrest,并且已經導入了 junit4 這個包。

  在 eclipse 中,對項目 testhamcrest 右鍵,選擇 build path / add external archives。

  選擇 /home/mark/tools/hamcrest-1.2 下面的 hamcrest-core-1.2.jar、hamcrest-library-1.2.jar 兩個 jar 檔案。

  項目中可以:

  import static org.hamcrest.matchers.*;

  那麼,就可以直接使用其中的方法。更多關于 hamcrest 的使用,請參閱文檔!

  4. 遇到麻煩

  trouble:    securityexception 異常

  methond:删除 junit 包,然後在 referenced  library 中再導入 junit 包。

最新内容請見作者的github頁:http://qaseven.github.io/