JUnit

assertEquals(a, b)
测试a是否等于bab是原始类型数值(primitive value)或者必须为实现比较而具有equal方法)
assertFalse(a)
测试a是否为false(假),a是一个Boolean数值。
assertNotNull(a)
测试a是否非空,a是一个对象或者null。
assertNotSame(a, b)
测试a和b是否没有都引用同一个对象。
assertNull(a)

      测试a是否为null,a是一个对象或者null。

  assertSame(a, b)  

测试a和b是否都引用同一个对象。

assertTrue(a)

测试a是否为true(真),a是一个Boolean数值。

创建测试应用程序

我们将在一个例子中使用JUnit来展示它如何在我们创建的应用程序JN_test中运行。此应用程序的意图是简单地为一个整型数组赋值,并通过getset方法来对它们进行存取。

特别地,我们将包含三个方法,它们用来返回可能被用在JUnit中测试的值。

allocate

分配一个整型数组并且对其赋值;返回新分配的数组。

set

为整型数组赋值;如果成功返回true,否则返回false。

get

从整型数组中取值;返回需要的数值。

JN_test的最初版本:

package net.csdn.blog;

public class JN_test {

    private int[] array;

    public int[] allocate()

    {

       

        array[0] = 0;

        array[1] = 1;

        array[2] = 2;

        return array;

    }

    public int get(int index){

        return array[index];

    }

    public boolean set(int index,int value){

        if(index<array.length && index >=0)

        {

            array[index] = value;

            return true;

        }

        else

            return false;

    }

}

allocate方法分配一个整型数组,对其进行赋值,并返回此数组:

private int[] array;

public int[] allocate( ) {
       array[0]=0;
       array[1]=1;
       array[2]=2;

       return array;

}

get方法在数组的给定位置取整数值:

public int get(int index) {

        return array[index];

}

set方法为给定的位置赋整数值,按条件返回true或者false。

public boolean set(int index, int value) {

        if (index < array.length && index >= 0) {
        array[index]=value;

        return true;

        } else {

                return false;

        }

}
上课的例子:::::::~
import junit.framework.*;
public class FindCharTest extends TestCase {
   private FindChar fc= null;
 private char[] chars = {'a','b','s','t'}; 
 protected void setUp() throws Exception {
  super.setUp();
 }
 public void testSearchFound() throws Exception
 {
  int indx=fc.search(chars,'s');
  assertEquals(indx,3);
 }
 public void testSearchTure() throws Exception
 {
  int indx=fc.search(chars,'q');
  assertTrue((indx== -1));
 }
 public void testem() throws Exception
 {
  char[] cha=null;
  fc.search(cha,'a');
  fail() ;
 }
 protected void tearDown() throws Exception {
  super.tearDown();
 }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值