单元测试实例 新建测试用例 public class Test extends AndroidTestCase { public void testMethod1()throws Throwable{ TestService t=new TestService(); (); } public void testMethod2()throws Throwable{ TestService t=new TestService(); assertEquals(56, ()); } } 说明:1、如果测试方法内部遇到异常,建议直接抛出,而不是捕获异常。异常抛出后会被 测试框架获取,之后在控制台显示出来,方便了解异常信息。 2、Assert类在此的作用是判断所得的结果和期望值的关系,在此例中如果相等则通过, 不相等,则抛出异常. public class TestService { public void method1(){ String s="111"; int a=new Integer(s);} public int method2(){ int i=1;int sum=0; while(i<=10){ sum=sum+i; i++;} return sum;} } 单元测试实例