JUnit测试框架之JUnit3和JUnit4使用区别的总结.doc_第1页
JUnit测试框架之JUnit3和JUnit4使用区别的总结.doc_第2页
JUnit测试框架之JUnit3和JUnit4使用区别的总结.doc_第3页
JUnit测试框架之JUnit3和JUnit4使用区别的总结.doc_第4页
JUnit测试框架之JUnit3和JUnit4使用区别的总结.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

JUnit测试框架之JUnit3和JUnit4使用区别的总结 (2010-05-15 20:15:20)转载标签:junit测试框架阿堂网络时空杂谈分类:java技术阿堂相信,做过java开发的朋友,对于JUnit这个开源测试框架,应该都不会陌生。没有用JUnit这款优秀的开源测试框架前,可能我们会更多使用main函数来进行一些形如System.out.println或System.err这些打印输出来作为测试方法。但是这样直接在程序中编写测试代码的做法会带来很多弊端。程序功能与测试功能相互耦合,不能分离,同时随着业务功能的增加,main函数会膨胀起来,不利于系统维护。对于实现同样的测试功能,使用JUnit工具可以使功能与测试分离,提高可维护性。下面,阿堂主要是要对比JUnit3和JUnit4进行编写测试用例时,一些区别的比较总结一下,相信会对朋友们使用JUnit编写测试用例时,一定会有所帮助的.假设现在有一个图书管理的类Library和一个图书类Book,在一个Library中可以存放多本书籍。具体类代码如下Book类代码如下package junit;public class Book private String title;private String author;private String introduce;public String getAuthor() return author;public void setAuthor(String author) this.author = author;public String getIntroduce() return introduce;public void setIntroduce(String introduce) roduce = introduce;public String getTitle() return title;public void setTitle(String title) this.title = title;Library类代码 如下package junit;import java.util.ArrayList;import java.util.List;public class Library private List bookList;public Library() this.bookList = new ArrayList();public void addBook(Book book) this.bookList.add(book);public boolean checkAvailabilityByTitle(String title) if(title=null)return false;for(Book tempBook:bookList)if(tempBook.getTitle().equals(title)return true;return false;用JUnit3编写的测试代码如下package junit.test;import org.junit.Assert;import junit.Book;import junit.Library;import junit.framework.TestCase;public class JUnit3LibraryTest extends TestCase private Library library;protected void setUp() throws Exception library=new Library();protected void tearDown() throws Exception library=null;public void testAddBook() Book masterSpring=new Book();masterSpring.setTitle(master spring);masterSpring.setAuthor(Sam);masterSpring.setIntroduce(about spring framework);library.addBook(masterSpring);boolean result=library.checkAvailabilityByTitle(master spring);Assert.assertEquals(The Library have the book master spring , true,result);public void testCheckAvailabilityByTitle() Book antBook=new Book();antBook.setTitle(Ant develop and integration reference);antBook.setAuthor(Sam);library.addBook(antBook);boolean result=library.checkAvailabilityByTitle(Ant develop and integration reference);Assert.assertEquals(The Library have the book Ant develop and integration reference, true,result);junit4编写的测试代码如下package junit.test;import junit.Book;import junit.Library;import org.junit.After;import org.junit.Assert;import org.junit.Before;import org.junit.Test;public class JUnit4LibraryTest private Library library;/使用标注声明这是初始化方法Beforepublic void beforeDoTest() throws Exception library=new Library();/使用Test标注,表明这是一个测试方法Testpublic void addBookToLibrary() Book masterSpring=new Book();masterSpring.setTitle(master spring);masterSpring.setAuthor(Sam);masterSpring.setIntroduce(about spring framework);library.addBook(masterSpring);boolean result=library.checkAvailabilityByTitle(master spring);Assert.assertEquals(The Library have the book master spring , true,result);Testpublic void bookAvailabilityInLibraryByTitle() Book antBook=new Book();antBook.setTitle(Ant develop and integration reference);antBook.setAuthor(Sam);library.addBook(antBook);boolean result=library.checkAvailabilityByTitle(Ant develop and integration reference);Assert.assertEquals(The Library have the book Ant develop and integration reference, true,result);/使用after标注,表明这是结束测试时要调用的方法Afterpublic void afterAllTest() throws Exception library=null;从上面的代码和运行中,我们可以总结junit3和junit4的使用区别如下1.在JUnit3中需要继承TestCase类,但在JUnit4中已经不需要继承TestCase2.在JUnit3中需要覆盖TestCase中的setUp和tearDown方法,其中setUp方法会在测试执行前被调用以完成初始化工作,而tearDown方法则在结束测试结果时被调用,用于释放测试使用中的资源,而在JUnit4中,只需要在方法前加上Before,就代表这个方法用于初始化操作,如上面的beforeDoTest方法,方法名是随意的3.在JUnit3中对某个方法进行测试时,测试方法的命令是固定的,例如对addBook这个方法进行测试,需要编写名字为tetAddBook的测试方法,而在JUnit4中没有方法命令的约束,例

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论