私はjUnitテストの初心者です。テストする必要がある以下のコードがあります。 add(Object val)とObject at(int index)をテストしようとしています。 どうすればそれらについて行くのですか?jUnitテスト(java)を使用して - void add(Object val)およびObject(int index)をテストするにはどうすればよいですか?
public class ResizingArray
{
// declare the one stack.
private Object[] Stack;
private int length; //array length
private int n; //number of elements
public ResizingArray(int n) //Constructor
{
//Create a new stack with n elements of ints.
Stack = new Object[n];
length = 0; //no items yet
}
//return numbers of elements in stack.
public int length()
{
return length;
}
//Return the object at the indicated index of the array.
public Object at(int index)
{
if(index < length)
{
return Stack[index];
}
else
{
throw new ArrayIndexOutOfBoundsException();
}
}
//add an object at the next available index
public void add(Object val)
{
//increment length
if(length() < Stack.length)
{
length++;
}
}
jUnit Test //This is the jUnit testing
import org.junit.Assert;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class ResizingArrayTest
{
protected ResizingArray newStack2;
public ResizingArrayTest()
{
}
atTest
public void testLength()
{
int[] newStack1 = new int[100]; //a new stack with 100 elements
//use the for loop to inialize 100 elements
for(int i = 0; i <= newStack1.length; ++i)
{
assertEquals((100),newStack1.length); //test for length.
newStack1.add(i+1); //test for add
}
}
}
この回答は役に立ちましたか?はいの場合は、助けになった答えを受け入れるように頼むことができますか? – notionquest