package com.test.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];
}
}
测试代码如下:
package com.test.junit;
import com.test.junit.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)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
String result = null;
try
{
result = myStack.pop();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Assert.assertEquals("hello world", result);
}
public void testPush2()
{
for (int i = 0; i < 100; ++i)
{
try
{
myStack.push(i + "");
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
for (int i = 0; i < 100; ++i)
{
String result = null;
try
{
result = myStack.pop();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Assert.assertEquals((99 - i) + "", result);
}
}
public void testPush3()
{
Throwable tx = null;
try
{
for (int i = 0; i <= 100; ++i)
{
myStack.push(i + "");
}
Assert.fail();
}
catch (Exception e)
{
e.printStackTrace();
tx = e;
}
Assert.assertNotNull(tx);
Assert.assertEquals(Exception.class, tx.getClass());
Assert.assertEquals("数组越界异常!", tx.getMessage());
}
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();
Assert.assertEquals("hello world", result);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
public void testTop2()
{
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 testDelete2()
{
Throwable tx = null;
try
{
for (int i = 0; i < 10; ++i)
{
myStack.push(i + "");
}
myStack.delete(11);
myStack.push("hello world");
}
catch (Exception ex)
{
ex.printStackTrace();
tx = ex;
}
Assert.assertNotNull(tx);
Assert.assertEquals(Exception.class, tx.getClass());
Assert.assertEquals("数组越界异常!", tx.getMessage());
}
}
分享到:
相关推荐
JUnit是由 Erich Gamma 和 Kent Beck 编写的一个回归测试框架(regression testing framework),供Java开发人员编写单元测试之用。
junit测试 test txt file
junit总结使用例子junit总结使用例子junit总结使用例子
junit测试测试junit测试测试junit测试测试junit测试测试
Junit 单元测试 包括Spring Struts 两个模块的测试案例。附源码
Junit 通常用于那些有明确返回值的方法的测试...因此我们要使用junit测试void方法,必须找到一个有实际返回值的 方法来暂时替代void方法,但此方法只作为被测void方法的入口和出口,测试程序所调用的仍旧是原void方法。
针对Calculate类 (含加减乘除4个方法),使用junit4进行简单的单元测试例子,含注释~
Junit,测试Junit,测试案例使用
软件测试JUnit测试框架的使用实验1报告
一个不错的关于junit4的测试例子,值得一看。
因此我们要使用junit测试void方法,必须找到一个有实际返回值的方法来暂时替代void方法,但此方法只作为被测void方法的入口和出口,测试程序所调用的仍旧是原void方法。为了实现这种测试方式,我们按照如下步骤进行...
Junit测试.mp4
Myeclipse中的JUnit测试详细
Junit测试jar包
单元测试框架Junit—— JUnit3.8.1 以及使用JUnit测试的代码demo。
eclipse下利用ant、junit进行自动化测试例子源码
junit3 junit4 api,单元测试的利器
Junit单元测试Junit单元测试Junit单元测试Junit单元测试
使用Eclipse进行Junit测试,junit类的创建
Ant+JUnit测试报告实际例子 资源分6分,希望物有所值而不是“无有所值”。