Eclipse中JUnit使用基础

 

Eclipse中已经集成JUnit的最新版本,JUnit的重要性可见一斑,下面简单介绍一下在EclipseJUnit的使用。

 

软件环境:

Eclipse Platform

Version: 3.0.2

Build id: 200503110845

 

此版本集成JUnit3.8.1

 

假设现在有一个类需要进行单元测试:

/*

 * Created on 2002-9-28 ,rsc

 *

 * @Author:Jonathan Q. Bo from tju.msnrl

 * MyBlog:http://blog.csdn.net/jonathan_q_bo

 *

 */

package org.tju.rsc.db;

 

import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

 

import net.sf.hibernate.Query;

import net.sf.hibernate.Session;

import net.sf.hibernate.type.LongType;

 

import org.springframework.dao.DataAccessException;

import org.springframework.orm.hibernate.support.HibernateDaoSupport;

import org.tju.rsc.entity.*;

 

/**

 * @author Administrator

 * 2002-9-28 7:29:16

 */

public class QuestionDb extends HibernateDaoSupport{

       public void add(TQuestion question) throws DataAccessException{

              this.getHibernateTemplate().save(question);

       }

      

       /**同主题删除*/

       public void deleteOne(long id) throws DataAccessException{

              this.getHibernateTemplate().delete("from TQuestion question where question.id = ?",new Long(id),new LongType());

       }

      

       /**单条删除*/

       public void delete(long ownerid) throws DataAccessException{

              this.getHibernateTemplate().delete("from TQuestion question where question.ownerid = ?",new Long(ownerid),new LongType());

       }

 

… ….

 

 

测试所有方法

 

 

1.     首先建立测试用例TestCase

2.     在左侧package explore窗口中右键选中QuestionDb.java文件,

3.     选择new->JUnit TestCase

4.     在向导中选择需要测试的函数,向导会自动添加test建立QuestionDbTest类。

5.     QuestionDbTest类中添加方法体,

6.     选择run->run as->JUnit test,会默认运行所有的测试方法。

 

共同成员变量的初始化与释放

 

 

如果这些测试方法都需要一些共同的对象,可重写TestCasesetUp()方法初始化成员变量,重写tearDown()方法释放成员变量,setUp()tearDown()只执行一次。

When you have a common fixture, here is what you do:

1.Create a subclass of TestCase

2.Add an instance variable for each part of the fixture

3.

Override setUp() to initialize the variables

 

4.

Override tearDown() to release any permanent resources you allocated in setUp

 

 

如何定制测试某些方法

 

 

每一个TestCase中,可能会测试多个方法,如果只想运行某一个或某几个方法,可添加如下方法:

       public static Test suite(){

              TestSuite test = new TestSuite("test QuestionDb");

              test.addTest(new QuestionDbTest("testDelete"));

              test.addTest(new QuestionDbTest("testDeleteOne"));

              return test;

       }

 

你可能疑惑,QuestionDbTest类并没有上面的构造函数,是的,需要添加如下构造函数:

       public QuestionDbTest(String method){

              super(method);

       }

 

再次运行,试一试,ok

 

如何组织运行所有测试用例

 

 

当我们建立了若干个TestCase之后,我们需要将其组织起来,一次执行多个TestCase

选择new->JUnit->Junit TestSuite,选择若干个TestCase,确定,这样我们就建立一个TestSuite

/*

 * Created on 2002-10-7 ,rsc

 *

 * @Author:Jonathan Q. Bo from tju.msnrl

 * MyBlog:http://blog.csdn.net/jonathan_q_bo

 *

 */

package org.tju.rsc.db;

 

import junit.framework.Test;

import junit.framework.TestSuite;

 

/**

 * @author Administrator

 * 2002-10-7 8:25:19

 */

public class AllTests1 {

 

       public static Test suite() {

              TestSuite suite = new TestSuite("Test for org.tju.rsc.db");

             

suite.addTestSuite(NoticeDbTest.class);

 

              suite.addTestSuite(RoleDbTest.class);

              suite.addTest(QuestionDbTest.suite());

              suite.addTestSuite(UploadDbTest.class);

              suite.addTestSuite(NewFiletypeDbTest.class);

              suite.addTestSuite(RecuitDbTest.class);

              suite.addTestSuite(NewsDbTest.class);

              suite.addTestSuite(FileDbTest.class);

              suite.addTestSuite(PolicyTypeDbTest.class);

              return suite;

       }

}

默认会执行所有TestCase的所有测试方法。

 

如何按照定制方式组织测试用例

 

 

如果只想运行某些TestCase中某几个方法,而不是所有方法,可首先在TestCase中添加public static Test suite()方法,确定要运行的测试方法,然后在suite()方法中添加suite.addTest(QuestionDbTest.suite());

/*

 * Created on 2002-10-7 ,rsc

 *

 * @Author:Jonathan Q. Bo from tju.msnrl

 * MyBlog:http://blog.csdn.net/jonathan_q_bo

 *

 */

package org.tju.rsc.db;

 

import junit.framework.Test;

import junit.framework.TestSuite;

 

/**

 * @author Administrator

 * 2002-10-7 7:26:26

 */

public class AllTests {

 

       public static Test suite() {

              TestSuite suite = new TestSuite("Test for org.tju.rsc.db");

             

suite.addTest(QuestionDbTest.suite());

 

              return suite;

       }

}

 

总结:

 

 

1、  首先建立单个类的测试用例

2、  在单个TestCase中通过public static Test suite()方法定制要测试的方法,不要忘了添加构造函数噢

3、  通过建立TestSuite来组织TestCase

4、  suite()方法中添加suite.addTestSuite(Test test)来组织执行TestCase的所有测试方法

5、  suite()方法中添加suite.addTest(TestCase.suite())来组织执行TestCase的所有测试方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值