Junit3.X 初学 (三)
来源:互联网 发布:伊戈达拉生涯数据 编辑:程序博客网 时间:2024/05/01 23:40
自定义一个栈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;@Overridepublic void setUp() throws Exception {// TODO Auto-generated method stubmystack = new MyStack();}public void testPush() {try {mystack.push("hello");} catch (Exception e) {// TODO Auto-generated catch blockAssert.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 blockth = 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 blockth = 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
- Junit3.X 初学 (三)
- Junit3.X 初学(二)
- Junit3.X 初学 (四) 如何测试类的静态Static方法
- Junit3.8 初学
- cocos2d-x 初学三问(上)
- cocos2d-x 初学三问(下)
- 三分钟掌握 JUnit3.0
- JUnit3
- 初学XML(三)
- 基于Junit3.8和Junit4.x的单元测试详解
- 初学S2SH搭建(三)
- 初学JNI知识(三)
- unity初学入门(三)
- maven初学(三) SNAPSHOT
- 新手初学dom4j(三)
- js初学笔记(三)
- ButterKnife初学手册(三)
- Java Servlet初学(三)
- 线段树扫描线upcoj2540
- HBase知识体系及伪分布安装
- oracle 全扫描和索引扫描
- 顺序栈的实现SqStack
- 九度oj 1972
- Junit3.X 初学 (三)
- LeetCode之reverse
- Java transient关键字使用小结及一些需要注意的细节
- Extjs grid checkbox 根据数据初始化选中
- Hibernate实战_笔记34(映射枚举、实现EnhanceUserType接口)
- think php 笔记
- linux c/c++ 聊天小程序代码示例 (使用socket 和 IO 多路复用技术)
- 使用SAX解析XML文档
- int *p = new int(5); 和 int *p = new int[5]的区别