package com.szys.junit; public class Packaging { int m; int n; public T(int m,int n) { try{ this.m=m; this.n=n; }catch(Exception e) { if(a<0||a>100||b<0||b>100) { System.out.ptintln("你输入的数不在范围内!"); } } } public int add() { return m + n; } public int minus() { return m - n; } public int mul() { return m * n; } public int div()throws Exception { if(0 == n) { throw new Exception("除数不能为0"); } return m / n; } }
package com.szys.junit.test; import static org.junit.Assert.*; import org.junit.Test; import com.szys.junit.T; public class Test { public void test() { int m=new T(1, 2).add(); assertEquals(3, m); int n=new T(4, 3).minus(); assertEquals(1,n); int x=new T(3, 4).mul(); assertEquals(12,x); try { int y=new T(4, 4).div(); assertEquals(1, y); }catch(Exception e){ e.printStackTrace(); } } }
加了try-catch语句。单元测试还是不太懂,有待提高!