对于我的类,我们创建ArrayStacks和LinkedStacks,而不是使用J-Unit进行测试。我们的一个测试是使用clear()方法。我们的教授特别要求我们清空堆栈中的每个元素,并测试它们是否为空。我该怎么做呢?J-Unit测试数组堆栈中的clear()方法
public void clear() {
// Checks if this stack is empty,
// otherwise clears this stack.
if(!isEmpty()){
for(int i = 0; i < sizeIs(); i++){
pop();
}
topIndex = -1;
}
}
public class Test_clear {
/*
* Class to test the clear method added to the Stack ADT of Lab04
*
* tests clear on an empty stack
* a stack with one element
* a stack with many (but less than full) elements
* and a "full" ArrayStack (not applicable to Linked Stack - comment it out)
*/
ArrayStack stk1, stk2;
@Before
public void setUp() throws Exception {
stk1 = new ArrayStack(); stk2 = new ArrayStack();
}
@Test
public void test_clear_on_an_emptyStack() {
stk1.clear();
Assert.assertEquals(true, stk1.isEmpty());
}
@Test
public void test_clear_on_a_stack_with_1_element() {
stk1.push(5);
stk1.clear();
Assert.assertEquals(true, stk1.isEmpty())'
}
等等。但是检查isEmpty()上的assertEquals将不会测试数组中的元素是否被清除。提前致谢!
该数组是'private',或者应该是。所以也请参阅http://stackoverflow.com/questions/34571/whats-the-proper-way-to-test-a-class-with-private-methods-using-junit – Raedwald