想飞就别怕摔

大爷的并TM骂人

Junit简单例子

本例是加减乘除计算
先看一个简单的类Calculator.java:
 1package com.test.junit;
 2/**
 3 * 
 4 * 数学计算
 5 *
 6 */

 7public class Calculator {
 8    public int add(int a,int b){
 9        return a + b;
10    }

11    public int minus(int a,int b){
12        return a - b;
13    }

14    public int multiply(int a, int b ){
15        return a * b;
16    }

17    public int divide(int a , int b )throws Exception
18    {
19        if(0 == b){
20            throw new Exception("除数不能为零");
21        }

22        return a / b;
23    }

24/*    
25 * 一下是不用IDE给我们提供的Junit,用Junit本身去测试的3个方法。
26    public static void main (String[]args){
27        junit.textui.TestRunner.run(CalculatorTest.class);
28        junit.swingui.TestRunner.run(CalculatorTest.class);
29        junit.awtui.TestRunner.run(CalculatorTest.class);
30    }
31*/

32}

下面我们看看传统的JUnit的TestCase:
CalculatorTest.java:
 1package com.test.junit;
 2
 3import junit.framework.Assert;
 4import junit.framework.TestCase;
 5/**
 6 * Keep the bar green to keep the code clean
 7 * 
 8 *在Junit3.8中必须继承TestCase类
 9 *
10 *单元测试不是证明您是对的,而是证明您没有错
11 */

12public class CalculatorTest extends TestCase {
13    /**
14     * 在Junit3.8中,测试方法满足如下原则
15     * 1)public
16     * 2)void 
17     * 3)无方法参数
18     * 4)最重要的方法名称必须以test开头
19     */

20    private Calculator cal;
21    
22    //在执行每个test之前,都执行setUp;
23    public void setUp(){
24         cal = new Calculator();
25    }

26    
27    //在执行每个test之后,都要执行tearDown
28    public void tearDown(){
29    }

30
31    public void testAdd()
32    {
33//        Calculator cal = new Calculator();
34        int result = cal.add(12);
35        //断言assert
36        Assert.assertEquals(3, result);
37    }
 
38    
39    public void testMinus()
40    {
41//        Calculator cal = new Calculator();
42        int result = cal.minus(52);
43        Assert.assertEquals(3, result);
44    }

45    
46    public void testMultiply()
47    {
48        Calculator cal = new Calculator();
49        int result = cal.multiply(42);
50        Assert.assertEquals(8,result);
51    }

52    
53    public void testDivide()
54    {
55//        Calculator cal = new Calculator();
56        int result = 0;
57        try {
58            result = cal.divide(10,5);
59        }
 catch (Exception e) {
60            e.printStackTrace();
61            //我们期望result = cal.divide(10,5);正常执行;如果进入到catch中说明失败;
62            //所以我们加上fail。
63            Assert.fail();//如果这行没有执行。说明这部分正确。
64        }

65        Assert.assertEquals(2,result);
66    }

67    
68    public void testDivide2()
69    {
70        Throwable tx = null
71        try 
72        {
73//            Calculator cal = new Calculator();
74            cal.divide(10,0);
75            //正常来讲cal.divide(10,0);已经抛出异常,之后的代码不会被执行。
76            //我们也期望是这样的。所以说如果下面的Assert.fail();执行了。
77            //我的的测试就失败了。
78            Assert.fail();//当执行到这里测试失败,后面的代码将不被执行。
79        }
 catch (Exception e) {
80            tx = e;
81        }

82        Assert.assertNotNull(tx);//断言tx不为空。也就是说肯定有异常。
83        Assert.assertEquals(Exception.class,tx.getClass());//断言tx的类型为Exception类型
84        Assert.assertEquals("除数不能为零", tx.getMessage());
85    }

86}

本例中只有一个测试类。不过在开发过程中随着项目的壮大,测试类也会越来越多。我们总不能每次测试都去挨个的执行每个test类吧。所以我们要写个总体的test类:
代码如下:
 1package com.test.junit;
 2
 3import junit.framework.Test;
 4import junit.framework.TestCase;
 5import junit.framework.TestSuite;
 6
 7public class TestAll extends TestCase {
 8    public static Test suite()
 9    {
10        TestSuite suite = new TestSuite();
11         suite.addTestSuite(CalculatorTest.class);//加入你所想测试的测试类
12         
13         return suite;
14    }

15}

posted on 2008-11-25 17:22 生命的绽放 阅读(5301) 评论(1)  编辑  收藏 所属分类: JUnit

评论

# re: Junit简单例子[未登录] 2012-07-14 09:32 啊啊

共 产 党  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2008年11月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

导航

统计

常用链接

留言簿(5)

随笔分类(94)

随笔档案(93)

文章分类(5)

文章档案(5)

相册

JAVA之桥

SQL之音

兄弟之窗

常用工具下载

积分与排名

最新评论

阅读排行榜