zoukankan      html  css  js  c++  java
  • Junit3.8

    使用Junit的最佳实践:新建一个名为test的source folder,用于存放测试类源代码目标类与测试类应该位于同一个包下面,这样测试类中就不必导入源代码所在的包,因为他们位于同一个包下面

    测试类的命名规则:假如目标类是Calculator,那么测试类应该命名为TestCalculator或者是CalculatorTest

    Junit:单元测试不是为了证明是对的,而是为了证明没有错误。

    测试用例(Test Case)是单元测试的一个很重要的方面。测试类必须要继承于TestCase父类。

    单元测试主要是用来判断程序的执行结果与自己期望的结果是否一致。

    在junit 3.8中,测试方法需要满足如下原则:public的,void的,无方法参数,方法名称必须以test开头.

    Test Case之间一定要保持完全的独立性,不允许出现任何的依赖关系。我们不能依赖于测试方法的执行顺序。

    关于setUp与tearDown方法的执行顺序:
    setUp()
    testXXX()//测试方法
    tearDown()

    测试之前是什么状态,测试执行完毕后就应该是什么状态,而不应该由于测试执行的原因导致状态发生了变化。

    代码示例:

      1 package junit;
      2 
      3 public class Largest
      4 {
      5     public int getLargest(int[] array) throws Exception
      6     {
      7         if(null == array || 0 == array.length)
      8         {
      9             throw new Exception("数组不能为空!");
     10         }
     11         
     12         int result = array[0];
     13         
     14         for(int i = 0; i < array.length; i++)
     15         {
     16             if(result < array[i])
     17             {
     18                 result = array[i];
     19             }
     20         }
     21         
     22         return result;
     23     }
     24 }
     25 
     26 
     27 
     28 /*
     29 *测试方法
     30 */
     31 package junit;
     32 
     33 import junit.framework.Assert;
     34 import junit.framework.TestCase;
     35 
     36 public class LargestTest extends TestCase
     37 {
     38     private Largest largest;
     39 
     40     public void setUp()
     41     {
     42         largest = new Largest();
     43     }
     44 
     45     public void testGetLargest()
     46     {
     47         int[] array = { 1, 9, -10, -20, 23, 34 };
     48 
     49         int result = 0;
     50 
     51         try
     52         {
     53             result = largest.getLargest(array);
     54         }
     55         catch (Exception ex)
     56         {
     57             Assert.fail("测试失败");
     58         }
     59 
     60         Assert.assertEquals(34, result);
     61     }
     62 
     63     public void testGetLargest2()
     64     {
     65         Throwable tx = null;
     66 
     67         int[] array = {};
     68 
     69         try
     70         {
     71             largest.getLargest(array);
     72 
     73             Assert.fail("测试失败");
     74         }
     75         catch (Exception ex)
     76         {
     77             tx = ex;
     78         }
     79 
     80         Assert.assertNotNull(tx);
     81 
     82         Assert.assertEquals(Exception.class, tx.getClass());
     83 
     84         Assert.assertEquals("数组不能为空!", tx.getMessage());
     85     }
     86 
     87     public void testGetLargest3()
     88     {
     89         Throwable tx = null;
     90 
     91         int[] array = null;
     92 
     93         try
     94         {
     95             largest.getLargest(array);
     96 
     97             Assert.fail();
     98         }
     99         catch (Exception ex)
    100         {
    101             tx = ex;
    102         }
    103 
    104         Assert.assertNotNull(tx);
    105 
    106         Assert.assertEquals(Exception.class, tx.getClass());
    107 
    108         Assert.assertEquals("数组不能为空!", tx.getMessage());
    109     }
    110 }
    View Code
  • 相关阅读:
    沉痛悼念乔布斯 (Steven Paul Jobs)
    Linux下文件属性
    window phone开发之动画效果
    简单的UDP收发讯息
    Red Hat Enterprise Linux 5 (红帽子企业版5)下Samba, VSFTP配置与安装
    XNA那些事(六)--WINDOWS PHONE 游戏开发中的3D摄像机
    今天发布iPhone 4s的可能性大一点吧(结果:iPhone4s+CDMA/GSM)
    iPhone开发:如何在iPhone应用中使用自定义字体
    C语言数组与指针详解
    linux 常用命令
  • 原文地址:https://www.cnblogs.com/daneres/p/4686664.html
Copyright © 2011-2022 走看看