JUnit3.8实现模拟堆栈的测试

首先是模拟的堆栈类MyStack.java

package com.jadyer.junit3; /** * 自定义一个Java的堆栈,然后对这个栈进行相关的测试 */ public class MyStack { private String[] elements; //使用字符串数组存放堆栈中的元素 private int nextIndex; //堆栈元素的指针,相当于数组下标。它代表所要压入的元素的位置 public MyStack() { elements = new String[100]; //限定堆栈中最多能压入100个元素 nextIndex = 0; //首先被压入的元素,它的位置是0 } /** * 把字符串压入堆栈顶部 */ public void push(String element) throws Exception { if (100 == nextIndex) { throw new Exception("数组越界异常!"); } elements[nextIndex] = element; nextIndex = nextIndex + 1; } /** * 移除堆栈顶部的元素,并作为此函数的值返回该元素 */ public String pop() throws Exception { if (0 == nextIndex) { throw new Exception("数组越界异常!"); } nextIndex = nextIndex - 1; return elements[nextIndex]; } /** * 删除堆栈中自栈顶开始的N个元素 */ public void delete(int n) throws Exception { if (nextIndex - n < 0) { throw new Exception("数组越界异常!"); } nextIndex = nextIndex - n; } /** * 查看堆栈顶部的元素,但不从堆栈中移除它 */ public String top() throws Exception { if (0 == nextIndex) { throw new Exception("数组越界异常!"); } return elements[nextIndex - 1]; } }

然后是使用JUnit3.8编写的单元测试类MyStackTest.java

package com.jadyer.junit3; import com.jadyer.junit3.MyStack; import junit.framework.Assert; import junit.framework.TestCase; public class MyStackTest extends TestCase { private MyStack myStack; public void setUp() { myStack = new MyStack(); } public void testPush() { try { myStack.push("hello world"); } catch (Exception e) { e.printStackTrace(); } String result = null; try { result = myStack.pop(); } catch (Exception e) { e.printStackTrace(); } //向堆栈中压入一个元素,然后再弹出来一个元素,二者做比较,以确定元素是否压入成功 Assert.assertEquals("hello world", result); } public void testPush22() { for (int i = 0; i < 100; ++i) { try { myStack.push(i + ""); //把整数转化为字符串 } catch (Exception e) { e.printStackTrace(); } } for (int i = 0; i < 100; ++i) { String result = null; try { result = myStack.pop(); } catch (Exception e) { e.printStackTrace(); } //向堆栈中压入100个元素,然后再弹出来100个元素,二者做比较,以确定元素是否压入成功 //压入时,第一个元素被压入到了栈底。所以弹出来的元素与压入元素的顺序正好是相反的 Assert.assertEquals((99 - i)+"", result); } } public void testPush33() { Throwable tx = null; try { for (int i = 0; i <= 100; ++i) { myStack.push(i + ""); //向堆栈中压入101个元素 } Assert.fail(); //该行代码不可以写在for循环里面 } catch (Exception e) { e.printStackTrace(); tx = e; } Assert.assertNotNull(tx); Assert.assertEquals(Exception.class, tx.getClass()); Assert.assertEquals("数组越界异常!", tx.getMessage()); } /** * 关于pop()方法,这里就不再进行其它测试了。因为其它测试也是与此类似的 */ public void testPop() { Throwable tx = null; try { myStack.pop(); Assert.fail(); } catch (Exception e) { e.printStackTrace(); tx = e; } Assert.assertNotNull(tx); Assert.assertEquals(Exception.class, tx.getClass()); Assert.assertEquals("数组越界异常!", tx.getMessage()); } public void testTop() { try { myStack.push("hello world"); String result = myStack.top(); //这里还应该再断言一次,即执行完top()之后堆栈中有多少个元素 //但是在MyStack.java中没有定义获得元素个数的方法,所以这里就暂时不去断言个数了 Assert.assertEquals("hello world", result); } catch (Exception ex) { ex.printStackTrace(); } } public void testTop22() { Throwable tx = null; try { myStack.top(); Assert.fail(); } catch (Exception e) { e.printStackTrace(); tx = e; } Assert.assertNotNull(tx); Assert.assertEquals(Exception.class, tx.getClass()); Assert.assertEquals("数组越界异常!", tx.getMessage()); } public void testDelete() { try { for (int i = 0; i < 10; ++i) { myStack.push(i + ""); } myStack.delete(10); } catch (Exception ex) { ex.printStackTrace(); Assert.fail(); } } public void testDelete22() { Throwable tx = null; try { for (int i = 0; i < 10; ++i) { myStack.push(i + ""); } myStack.delete(11); //delete()所影响的仅仅是nextIndex的值,而没有对数组本身做任何变化,所以本行代码不会出现错误 myStack.push("hello world"); //此时nextIndex值为-1,若再压入一个元素的话,肯定会出错,因为数组下标不能为-1 } catch (Exception ex) { ex.printStackTrace(); tx = ex; } Assert.assertNotNull(tx); Assert.assertEquals(Exception.class, tx.getClass()); Assert.assertEquals("数组越界异常!", tx.getMessage()); } }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值