我是jUnit测试的新手,我有以下代码需要测试。我试图测试add(Object val)和Object at(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
}
}
}
答案 0 :(得分:0)
通常,我们可以使用Mockito
或PowerMock
库来测试void方法。但是,add()
和at()
方法可以按如下方式进行测试: -
请注意,这只是一个示例测试。如果需要,您可以扩展以涵盖许多场景。
<强>概念: - 强>
add() - 如果add()
方法成功执行,则应增加长度。所以断言长度值。
at() - 如果at()
方法成功执行,则应返回该对象。因此,断言对象值。
构造函数和add()都没有将对象值赋给Stack变量。因此,如果at()
没有超出索引,则public class ResizingArrayTest {
@Test
public void addTestString() {
ResizingArray resizingArray = new ResizingArray(2);
resizingArray.add(new String("Str1"));
Assert.isTrue(resizingArray.length() == 1);
resizingArray.add(new String("Str2"));
Assert.isTrue(resizingArray.length() == 2);
//Max only 2 can be added as per the ResizingArray object creation
resizingArray.add(new String("Str3"));
Assert.isTrue(resizingArray.length() == 2);
}
@Test
public void addTestDoesnotAddString() {
ResizingArray resizingArray = new ResizingArray(0);
//Nothing can be added
resizingArray.add(new String("Str1"));
Assert.isTrue(resizingArray.length() == 0);
}
@Test
public void atTest() {
ResizingArray resizingArray = new ResizingArray(1);
resizingArray.add(new String("Str1"));
Assert.isNull(resizingArray.at(0));
}
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void atTestThrowException() {
ResizingArray resizingArray = new ResizingArray(0);
resizingArray.at(1);
}
}
始终返回null。
div.right
答案 1 :(得分:0)
好的,首先我认为你应该改变一下add(Object)方法,以便你实际上将Object添加到数组中:
//add an object at the next available index
public void add(Object val)
{
//increment length
if(length() < Stack.length)
{
Stack[length] = val;
length++;
}
}
其次,您还应检查何时检索传递的索引不为负值的值:
//Return the object at the indicated index of the array.
public Object at(int index)
{
if(index >= 0 && index < length)
{
return Stack[index];
}
else
{
throw new ArrayIndexOutOfBoundsException();
}
}
然后我们去测试。 我测试方法的方法是尝试为每个可能的场景创建单独的单元测试。由于您的测试代码变得清晰,因此更易于维护并符合单一责任原则。
所以他就是我要运行的测试套件:
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Before;
import org.junit.Test;
public class ResizingArrayTest {
private ResizingArray resizingArray;
private int defaultArraySize;
Integer valueToAddOne = Integer.valueOf(1);
Integer valueToAddTwo = Integer.valueOf(2);
Integer valueToAddThree = Integer.valueOf(3);
@Before
public void init(){
defaultArraySize = 2;
resizingArray = new ResizingArray(defaultArraySize);
}
@Test
public void shouldAddValueToTheStack() throws Exception{
// Act
resizingArray.add(valueToAddOne);
// Assert
assertThat("Array Length", resizingArray.length(), equalTo(1));
assertThat("Top Value", (Integer)resizingArray.at(resizingArray.length()-1), equalTo(valueToAddOne));
// Act
resizingArray.add(valueToAddTwo);
// Assert
assertThat("Array Length", resizingArray.length(), equalTo(2));
assertThat("Top Value", (Integer)resizingArray.at(resizingArray.length()-1), equalTo(valueToAddTwo));
}
@Test
public void shouldNotAddValueToTheStack() throws Exception{
// Arrange
resizingArray.add(valueToAddOne);
resizingArray.add(valueToAddTwo);
// Act
resizingArray.add(valueToAddThree);
// Assert
assertThat("Top Value", (Integer) resizingArray.at(resizingArray.length() - 1), equalTo(valueToAddTwo));
}
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void shouldThrowException_WhenRetrievingValueByNegativeIndex() throws Exception{
// Act
resizingArray.at(-1);
}
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void shouldThrowException_WhenRetrievingValueByIndexGreaterThanStackSize() throws Exception{
// Act
resizingArray.at(3);
}
@Test
public void shouldRetrieveValueByIndex() throws Exception{
// Arrange
resizingArray.add(valueToAddOne);
resizingArray.add(valueToAddTwo);
// Act
Integer firstElement = (Integer) resizingArray.at(0);
Integer secondElement = (Integer) resizingArray.at(1);
// Assert
assertThat("First element", (Integer) resizingArray.at(0), equalTo(firstElement));
assertThat("Second element", (Integer) resizingArray.at(1), equalTo(secondElement));
}
}
关键点很少: