JUnit测试框架之JUnit3和JUnit4使用区别的总结

时间:2022-11-10 05:08:00

 堂相信,做过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) {
  this.introduce = 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<Book> bookList;

 public Library() {
  this.bookList = new ArrayList<Book>();
 }
 
 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;
 //使用标注声明这是初始化方法
 @Before
 public void beforeDoTest() throws Exception {
  library=new Library();
 }
 //使用@Test标注,表明这是一个测试方法
 @Test
 public 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);
 }
 @Test
 public 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标注,表明这是结束测试时要调用的方法
 @After
 public void afterAllTest() throws Exception {
  library=null;
 }
}

JUnit测试框架之JUnit3和JUnit4使用区别的总结

JUnit测试框架之JUnit3和JUnit4使用区别的总结





从上面的代码和运行中,我们可以总结junit3和junit4的使用区别如下
1.在JUnit3中需要继承TestCase类,但在JUnit4中已经不需要继承TestCase
2.在JUnit3中需要覆盖TestCase中的setUp和tearDown方法,其中setUp方法会在测试执行前被调用以完成初始化工作,而tearDown方法则在结束测试结果时被调用,用于释放测试使用中的资源,而在JUnit4中,只需要在方法前加上@Before,就代表这个方法用于初始化操作,如上面的beforeDoTest方法,方法名是随意的
3.在JUnit3中对某个方法进行测试时,测试方法的命令是固定的,例如对addBook这个方法进行测试,需要编写名字为tetAddBook的测试方法,而在JUnit4中没有方法命令的约束,例如对addBook这个方法进行测试,那么可以编写addBookToLibrary的方法,然后在这个方法的前面加上@Test,这就代表这个方法是测试用例中的测试方法
4.编写JUnit4的测试用例和编写一个普通的类没有什么区别,只是需要加上Annotation指定要测试的方法,这种松偶合的设计理念相当优秀,能很好把测试分离出来.使用JUnit4的Annotation功能,需要JDK 1.5或以上版本

文章出处:http://zhang8mss.blog.163.com/blog/static/11046375620104158150751/