测试a是否为null,a是一个对象或者null。
测试a和b是否都引用同一个对象。
assertTrue(a)
测试a是否为true(真),a是一个Boolean数值。
创建测试应用程序
我们将在一个例子中使用JUnit来展示它如何在我们创建的应用程序JN_test中运行。此应用程序的意图是简单地为一个整型数组赋值,并通过get和set方法来对它们进行存取。
特别地,我们将包含三个方法,它们用来返回可能被用在JUnit中测试的值。
分配一个整型数组并且对其赋值;返回新分配的数组。
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;
return array;
}
get方法在数组的给定位置取整数值:
public int get(int index) {
return array[index];
}
set方法为给定的位置赋整数值,按条件返回true或者false。
public boolean set(int index, int value) {
return true;
} else {
return false;
}
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() ;
}
super.tearDown();
}