本文主要是介绍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;@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界面显示:
这篇关于Junit3.X 初学 (三)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!