對於我的類,我們創建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