4. 使用例子:
import junit.frmework.TestCase;
public class TestSample extends TestCaset{
protected void setUp(){
//初始化……
}
public void testMethod1(){
assertTrue( true);
}
potected void tearDown(){
//撤销初始化……
}
}
5. 区分fail、exception。
- fail,期望出现的错误。产生原因:assert函数出错(如assertFalse(true));fail函数产生(如fail(……))。
- exception,不期望出现的错误,属于unit test程序运行时抛出的异常。它和普通代码运行过程中抛出的runtime异常属于一种类型。
对于assert、fail等函数请参见junit的javadoc。
6. 使用例子:
import junit.frmework.TestCase;
public class TestSample extends TestCaset{
protected void setUp(){
//初始化……
}
public void testMethod1(){
……
try{
boolean b= ……
assertTrue( b);
throw new Exception( “This is a test.”);
fail( “Unable point.”); //不可能到达
}catch(Exception e){
fail( “Yes, I catch u”); //应该到达点
}
……
}
potected void tearDown(){
//撤销初始化……
}
}