日出星辰

          JUint測試

          ClassA.java

          public class ClassA implements Serializable {
          
          	private static final long serialVersionUID = 6013572251564847381L;
          	private String name = "My name is a";
          	private ClassB b = null;
          
          	ClassA() {
          		b = new ClassB();
          	}
          
          	public String show() {
          
          		System.out.println("a.toString <a.name=\"" + this.name
          				+ "\" a.b.name=\"" + this.b.getName() + "\">");
          
          		return "a.toString <a.name=" + this.name + " a.b.name="
          				+ this.b.getName() + ">";
          		// \" 雙引號
          		// \' 單引號
          		// \\ 反斜線
          	}
          
          	public int add(int a, int b) {
          		return a + b;
          	}
          
          	public int divide(int a, int b) throws Exception {
          		if (0 == b) {
          			throw new Exception("除數不能為零");
          		}
          		return a / b;
          	}
          
          	public int exception(int a, int b) throws Exception {
          		if (a == 2) {
          			throw new Exception("進入我設置的異常啦!");
          		}
          		return a + b;
          	}
          
          	public String getName() {
          		return name;
          	}
          
          	public void setName(String name) {
          		this.name = name;
          	}
          
          	public ClassB getB() {
          		return b;
          	}
          
          	public void setB(ClassB b) {
          		this.b = b;
          	}
          
          }
          

          ClassB.java

          public class ClassB implements Serializable{
          
          	private static final long serialVersionUID = -4324044767844361076L;
          
          	private String name="My name is b";
          	
          	ClassB(){}
          
          	public String getName() {
          		return name;
          	}
          
          	public void setName(String name) {
          		this.name = name;
          	}
          	
          	
          }
          

          JUin測試

          public class ClassATest extends TestCase {
          
          	ClassA a = new ClassA();
          
          	public void testShow() {
          		// 覆蓋原來的name
          		a.setName("hello TestA");
          		a.getB().setName("hello TestB");
          		a.show();
          	}
          
          	public void testAdd() {
          		// boolean b=false;
          		// Assert.assertEquals(true, b);
          		int result = 0;
          		result = a.add(2, 5);
          		Assert.assertEquals(7, result);
          	}
          
          	public void testDivide() {
          		int result = 0;
          		try {
          			result = a.divide(10, 5);
          		} catch (Exception e) {
          			e.printStackTrace(); // 如果進入到catch中說明執行divide失敗
          			System.out.println("執行上面那句話了");
          			Assert.fail();
          		}
          		Assert.assertEquals(2, result);
          	}
          
          	public void testException() {
          		int result = 0;
          		try {
          			result = a.exception(3, 4);
          		} catch (Exception e) {
          			e.printStackTrace();
          			System.out.println("執行上面那句話了");
          			Assert.fail();
          		}
          		Assert.assertEquals(7, result);
          	}
          }
          

           

           

           

          posted on 2011-08-23 15:36 日出星辰 閱讀(174) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 五指山市| 东光县| 阜城县| 儋州市| 资兴市| 蚌埠市| 独山县| 吉木萨尔县| 眉山市| 湟中县| 安西县| 松江区| 许昌县| 从化市| 正安县| 南郑县| 龙门县| 崇州市| 永顺县| 石渠县| 光泽县| 阿拉善左旗| 乌拉特前旗| 扎赉特旗| 平山县| 玉溪市| 顺昌县| 宁强县| 安丘市| 博乐市| 修武县| 达孜县| 永清县| 灵宝市| 万州区| 安西县| 武清区| 克什克腾旗| 武冈市| 南木林县| 花莲市|