Junit3.X 初学 (三)

自定义一个栈Stack, 实现栈的主要方法:push pop  top  delete 四种,并用测试类的9个测试用例来测试这4个方法:


待测试类:

package com.sysu.junit3;

import com.exception.sysu.MyException;

public class MyStack {

	private int pointer;
	private String[] arrayStack;

	public int getPointer() {
		return pointer;
	}

	public void setPointer(int pointer) {
		this.pointer = pointer;
	}

	public String[] getArrayStack() {
		return arrayStack;
	}

	public void setArrayStack(String[] arrayStack) {
		this.arrayStack = arrayStack;
	}

	public MyStack() {
		pointer = 0;
		arrayStack = new String[100];
	}

	public void push(String str) throws Exception {
		if (pointer == 100)
			throw new Exception("Stack is Full");
		arrayStack[pointer++] = str;
	}

	public String pop() throws Exception {
		if (pointer <= 0)
			throw new Exception("Stack is empty,");
		return arrayStack[--pointer];
	}

	public String top() throws Exception {
		if (pointer == 0)
			throw new MyException("My:Stack is Empty");

		return arrayStack[pointer - 1];
	}

	public void delete(int n) throws MyException {
		if (pointer - n < 0)
			throw new MyException("My:My:Stack is Empty when delete");
		pointer = pointer - n;
	}
}

测试类:

package com.sysu.junit3;

import junit.framework.Assert;
import com.exception.sysu.MyException;
import junit.framework.TestCase;

public class TestMyStack extends TestCase {

	private MyStack mystack = null;

	@Override
	public void setUp() throws Exception {
		// TODO Auto-generated method stub
		mystack = new MyStack();

	}

	public void testPush() {
		try {
			mystack.push("hello");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Assert.fail();
		}
		Assert.assertEquals("hello",
				mystack.getArrayStack()[mystack.getPointer() - 1]);

	}

	public void testPush2() {

		Throwable th = null;

		try {
			for (int i = 0; i < 100; i++)
				mystack.push("hello" + i);
		} catch (Exception e) {
			Assert.fail();
		}
		try {
			mystack.push("hey");
			Assert.fail();
		} catch (Exception e) {
			th = e;
		}
		Assert.assertNotNull(th);
		Assert.assertEquals("Stack is Full", th.getMessage());
		Assert.assertEquals("hello32", mystack.getArrayStack()[32]);
		Assert.assertEquals("hello99", mystack.getArrayStack()[99]);
		Assert.assertEquals("hello0", mystack.getArrayStack()[0]);
		Assert.assertEquals("hello94", mystack.getArrayStack()[94]);
		Assert.assertEquals("hello1", mystack.getArrayStack()[1]);
		Assert.assertEquals("hello98", mystack.getArrayStack()[98]);

	}

	public void testPop() {
		Throwable th = null;
		try {
			mystack.pop();
			Assert.fail();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			th = e;

		}
		Assert.assertNotNull(th);
		Assert.assertEquals(Exception.class, th.getClass());
		Assert.assertEquals("Stack is empty,", th.getMessage());
	}

	public void testPop2() {
		String string = null;
		try {
			for (int i = 0; i < 100; i++) {
				mystack.push(i + "");
			}
		} catch (Exception ex) {
			Assert.fail();
		}

		for (int i = 0; i < 100; i++) {
			try {
				string = mystack.pop();
			} catch (Exception ex) {
				Assert.fail();
			}
			Assert.assertEquals(99 - i + "", string);
		}

	}

	public void testPop3() {
		Throwable th = null;
		try {
			mystack.push("hello man");
		} catch (Exception ex) {
			Assert.fail();
		}
		try {
			mystack.pop();
			mystack.pop();
			Assert.fail();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			th = e;

		}
		Assert.assertNotNull(th);
		Assert.assertEquals(Exception.class, th.getClass());
		Assert.assertEquals("Stack is empty,", th.getMessage());
	}

	public void testTop() {
		String string = null;
		try {
			mystack.push("huawei");
		} catch (Exception e) {
			Assert.fail();
		}
		try {
			string = mystack.top();
		} catch (Exception e) {
			Assert.fail();
		}
		Assert.assertEquals("huawei", string);
	}

	public void testTop2() {
		for (int i = 0; i < 100; i++) {
			String string = null;
			try {
				mystack.push(i + "");

			} catch (Exception e) {
				Assert.fail();
			}
			try {
				string = mystack.top();
			} catch (Exception e) {
				Assert.fail();
			}
			Assert.assertEquals(i + "", string);
		}
	}

	public void testTopWithException() {
		Throwable th = null;
		try {
			mystack.top();
			Assert.fail();
		} catch (Exception e) {
			th = e;
		}
		Assert.assertNotNull(th);
		Assert.assertEquals(MyException.class, th.getClass()); // 使用自定义的MyException

	}

	public void testDelete() {
		Exception ex = null;
		try {
			for (int i = 0; i < 10; i++)
				mystack.push(i + "");
		} catch (Exception e) {
			Assert.fail();
		}
		try {
			mystack.delete(11);
			Assert.fail();
		} catch (Exception e) {
			ex = e;
		}
		Assert.assertEquals(MyException.class, ex.getClass());
		Assert.assertNotNull(ex);
		Assert.assertEquals("My:My:Stack is Empty when delete", ex.getMessage());

	}

	public static void main(String[] args) {

		junit.awtui.TestRunner.run(TestMyStack.class);

	}

}

测试结果,用junit自带的awt 的UI界面显示:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值