JUnit: Hamcrest使用

来源:互联网 发布:商品上淘宝首页 编辑:程序博客网 时间:2024/05/29 17:53

转自:http://blog.csdn.net/androidbluetooth/article/details/6406208


hamcrest是一款比较不错的JUnit测试jar包,本文介绍hamcrest在Java语言的使用。如果你使用过eclipse和JUnit4的话,hamcrest的使用会让你如虎添翼。

 

1. 下载hamcrest

 

连接Google,搜索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这个包。如果没有的话,请参考其他关于JUnit的使用的文章。

 

在Eclipse中,对项目TestHamcrest右键,选择Build Path / Add External Archives。

 

选择/home/mark/tools/hamcrest-1.2下面的hamcrest-core-1.2.jar、hamcrest-library-1.2.jar两个jar文件。

 

项目中可以

 

  1. import static org.hamcrest.Matchers.*;  
 

 

那么,就可以直接使用其中的方法。更多关于hamcrest的使用,请参阅文档!

 

4. 遇到麻烦

 

trouble:    SecurityException异常

 

methond:删除JUnit包,然后在Referenced  Library中再导入Junit包。


原创粉丝点击