JUNITでスタックをテストする例
5128 ワード
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());
}
}