日出星辰

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 日出星辰 阅读(172) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: