JUnit测试框架使用介绍(上)

发表于:2009-5-07 14:37

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:51cto博客

分享:

  一个TestCase测试实例

  下面是一个数字功能类,它提供了求最大值函数和求最小值函数:

package com.zj.c01;
 
public class NumberTool {
    public static int getMax(int[] arr) {
       int max = Integer.MIN_VALUE;
        if (arr.length == 0)
           throw new RuntimeException("Empty list");
       for (int index = 0; index < arr.length; index++) {
           if (arr[index] > max)
              max = arr[index];
       }
       return max;
    }
 
    public static int getMin(int[] arr) {
       int min = Integer.MAX_VALUE;
       if (arr.length == 0)
           throw new RuntimeException("Empty list");
       for (int i = 0; i < arr.length; i++) {
           if (arr[i] < min)
              min = arr[i];
       }
       return min;
    }
}

  下面针对求最大值函数编写测试用例:

  1.简单测试:[7,8,9]->9;

  2.位序测试:[9,8,7] ->9;[7,9,8] ->9;[8,7,9] ->9;

  3.重复值测试:[9,7,9,8] ->9;

  4.单值测试:[1]->1;

  5.负值测试:[-7,-8,-9]->-7;

  6.空值测试:[]->抛出异常;

  测试类NumberToolTest:

package com.zj.c01;
import junit.framework.TestCase;
 
public class NumberToolTest extends TestCase {
    public NumberToolTest(String name) {
       super(name);
    }
 
    public void testSimple() {
       assertEquals(9, NumberTool.getMax(new int[] { 7, 8, 9 }));
    }
 
    public void testOrder() {
       assertEquals(9, NumberTool.getMax(new int[] { 9, 8, 7 }));
       assertEquals(9, NumberTool.getMax(new int[] { 7, 9, 8 }));
       assertEquals(9, NumberTool.getMax(new int[] { 8, 7, 9 }));
    }
 
    public void testDups() {
       assertEquals(9, NumberTool.getMax(new int[] { 9, 7, 9, 8 }));
    }
 
    public void testOne() {
       assertEquals(1, NumberTool.getMax(new int[] { 1 }));
    }
 
    public void testNegitave() {
       assertEquals(-7, NumberTool.getMax(new int[] { -7, -8, -9 }));
    }
 
    public void testEmpty() {
       try {
           NumberTool.getMax(new int[] {});
           fail("Should have thrown an exception");
       } catch (RuntimeException e) {
           assertTrue(true);
       }
    }
}

  使用Eclipse-Run As JUnit Test

相关阅读:

JUnit测试框架使用介绍(下)

22/2<12
精选软件测试好文,快来阅读吧~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号