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());
}
}