天天看点

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/