For my class we are creating ArrayStacks and LinkedStacks, than practicing testing with J-Unit. One of our tests is on the clear() method. Our professor specifically asked that we null out each element in our stacks, and than test that they are null. How do I do this?
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())'
}
and so on. But checking assertEquals on isEmpty() won't test if the elements in my array are cleared or not. Thanks in advance!