JUnit4介绍及其快速使用

http://tech.it168.com/a2008/1231/261/000000261826_1.shtml

 

现在已经了解了JUnit3.8.1的使用和其基本的工作原理,JUnit 4是JUnit框架有史以来的最大改进,其主要目标便是利用Java 5的Annotation特性简化测试用例的编写。下面同样通过代码来学习一下:

  1.右击该类,选择 新建->JUnit测试用例,选择JUnit4,setUp和tearDown方法,点击下一步,选择需要测试的方法,JUnit会自动生成测试的代码框架(可以看到这个代码框架和上面的有较大的不同),手动添加自己的测试代码后如下:

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class HelloTest4 {        //这里不需要继承自TestCase

    private Hello hello;
    public HelloTest4()
    {
        super();
        System.out.println("a new test instance...");
    }
    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.println("call before all tests...");
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.println("call after all tests... ");
    }
    @Before
    public void setUp() throws Exception {
        //这里不需要调用super.setUp()
        System.out.println("call before test...");
        hello=new Hello();
    }

    @After
    public void tearDown() throws Exception {
        //这里不需要调用super.tearDown()
        System.out.println("call after test... ");
    }

    @Test
    public void testAbs() {
        System.out.println("test the method abs()");
        assertEquals(16, hello.abs(16));
        assertEquals(11, hello.abs(-10));//在这里,会出现故障,应该把左边的参数改为10
        assertEquals(0, hello.abs(0));
    }

    @Test
    public void testDivision() {
        System.out.println("test the method division()");
        assertEquals(3D, hello.division(6, 2));
        assertEquals(6D, hello.division(6, 1));
        assertEquals(0D, hello.division(6, 0));//在这里,会出现故障(与3.8.1有些不同?)
    }

    //下面,并不是对JunitDemo类中成员函数的测试,只是演示JUnit的一些功能
    
    //测试是否会发生期望的异常
    @Test(expected=ArithmeticException.class)
    public void testDiv0() {
        System.out.println("test the method Div0()");
        double result=100/0;
    }
    
    //测试是否超时
    @Test(timeout=1)
    public void testLongTimeTask() 
    {
        System.out.println("test the method LongTimeTask()");
        double d = 0;
        for(int i=1; i<10000000; i++)
            d+=i;
    }
    
}

  2.运行该测试类,输出如下:

call before all tests...
a new test instance...
call before test...
test the method abs()
call after test...

a new test instance...
call before test...
test the method division()
call after test...

a new test instance...
call before test...
test the method Div0()
call after test...

a new test instance...
call before test...
test the method LongTimeTask()
call after test...

call after all tests...
  3.从上面的输出结果可以看出,JUnit的工作原理和以前的几乎还是没有变的,只是让用户使用更简单了当然有一个变化是3.8.1的所有测试实例是测试前全都创建好的,而JUnit4的测试实例是在每个测试前创建的。

  4.当JUnit4还有一个与以前很大的不同就是引入了@BeforeClass和@AfterClass(可以在选择setUp和tearDown的时候选择setUpBeforeClass()和tearDownAfterClass()),setUpBeforeClass()在所有测试前调用,tearDownAfterClass()在所有测试后调用,它们不同与setUp和tearDown,在整个测试过程中只会被调用一次,这是为了能在@BeforeClass中初始化一些昂贵的资源,例如数据库连接,然后执行所有的测试方法,最后在@AfterClass中释放资源。

  总结,这里只是对JUnit对class的单元测试作了简单的讨论,除此以外,JUnit还可以对JSP,Servlt,EJB等做单元测试。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值