单元测试示例(三)

1.被测试类:

package com.ebuair.junit;

public class MyStack {
	private String[] elements;
	private int nextIndex;
	public MyStack() {
		elements = new String[100];
		nextIndex = 0;
	}
	public void push(String element) throws Exception{
		if(100 ==nextIndex){
			throw new Exception("数组越界异常");
		}
		elements[nextIndex++] = element;
	}
	public String pop() throws Exception{
		if(0 == nextIndex){
			throw new Exception("数组越界异常");
		}
		return elements[--nextIndex];
	}
	public void delete(int n) throws Exception{
		if(nextIndex - n < 0){
			throw new Exception("数组越界异常");
		}
		nextIndex -= n;
	}
	public String top() throws Exception{
		if(0 == nextIndex){
			throw new Exception("数组越界异常");
		}
		return elements[nextIndex - 1];
	}
}

2.测试类

package com.ebuair.junit;

import junit.framework.Assert;
import junit.framework.TestCase;

public class TestMyStack extends TestCase{

	private MyStack myStack= null;
	@Override
	public void setUp() throws Exception {
		myStack = new MyStack();
	}
	public void testPush(){
		String result = null;
		try {
			myStack.push("hello world");
		} catch (Exception e) {
			Assert.fail("数组越界异常");
		}
		try {
			result = myStack.pop();
		} catch (Exception e) {
			Assert.fail("数组越界异常");
		}
		Assert.assertNotNull(myStack);
		Assert.assertEquals("hello world", result);
	}
	public void testPush1(){
		String result = "";
		for(int i = 0; i < 100; i++){
			try {
				myStack.push(i + "");
			} catch (Exception e) {
				Assert.fail("数组越界异常");
			}
		}
		for(int i = 99; i >= 0; i--){
			try {
				result = myStack.pop();
			} catch (Exception e) {
				Assert.fail("数组越界异常");
			}
			Assert.assertEquals(i + "", result);
		}
	}
	public void testPush3(){
		Throwable throwable = null;
		try {
			for(int i = 0; i <=100; i++){
				myStack.push(i + "");
			}
			Assert.fail("数组越界异常");
		} catch (Exception e) {
			throwable = e;
		}
		assertData(throwable);
	}
	public void testPop(){
		String string = "hello world";
		String result = "";
		try {
			myStack.push(string);
		} catch (Exception e) {
			Assert.fail("数组越界异常");
		}
		try {
			result = myStack.pop();
		} catch (Exception e) {
			Assert.fail("数组越界异常");
		}
		Assert.assertNotNull(myStack);
		Assert.assertEquals("hello world", result);
	}
	public void testPop1(){
		Throwable throwable = null;
		try {
			myStack.pop();
			Assert.fail("数组越界异常");
		} catch (Exception e) {
			throwable = e;
		}
		assertData(throwable);
	}
	public void testPop2(){
		Throwable throwable = null;
		try {
			myStack.push("hello world");
		} catch (Exception e) {
			Assert.fail("数组越界异常");
		}
		try {
			myStack.pop();
			myStack.pop();
			Assert.fail();
		} catch (Exception e) {
			throwable = e;
		}
		assertData(throwable);
	}
	public void testTop(){
		String result = null;
		try {
			myStack.push("hello world");
			result = myStack.top();
		} catch (Exception e) {
			Assert.fail("数组越界异常");
		}
		Assert.assertNotNull(myStack);
		Assert.assertEquals("hello world", result);
	}
	public void testTop2(){
		Throwable throwable = null;
		String result = null;
		try {
			result = myStack.top();
			Assert.fail("数组越界异常");
		} catch (Exception e) {
			throwable = e;
		}
		assertData(throwable);
		}
	public void testDelete(){
		try {
			for(int i = 0; i < 10; i++){
				myStack.push(i + "");
			}
			myStack.delete(10);
		} catch (Exception e) {
			Assert.fail("数组越界异常");
		}
	}
	public void testDelete2(){
		Throwable throwable = null;
		try {
			for(int i = 0; i < 10; i++){
				myStack.push(i + "");
			}
			myStack.delete(11);
			Assert.fail("数组越界异常");
		} catch (Exception e) {
			throwable = e;
		}
		assertData(throwable);
	}
	private void assertData(Throwable throwable) {
		Assert.assertNotNull(throwable);
		Assert.assertEquals(Exception.class, throwable.getClass());
		Assert.assertEquals("数组越界异常", throwable.getMessage());
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值