这是数据结构部分:
public class SLList {
public class IntNode {
public int item;
public IntNode next;
public IntNode(int item, IntNode next){
this.item = item;
this.next = next;
}
}
private IntNode first;
public SLList(int x){
this.first = new IntNode(x, null);
}
public void addFirst(int n){
first = new IntNode(n, first);
}
public int getFirst(){
return first.item;
}
}
现在,我需要测试两个方法,addFirst() 和 getFirst()。我把代码写成:
public class SLListTest {
@Test
public void addFirst() {
SLList s = new SLList(10);
s.addFirst(5);
Assert.assertEquals(5, s.getFirst());
}
@Test
public void getFirst() {
SLList s = new SLList(10);
s.addFirst(5);
Assert.assertEquals(5, s.getFirst());
}
}
现在,我在这里看到一个问题,测试中对其他函数的调用。例如,对 addFirst() 的测试取决于 getFirst() 是否正确,以及与 getFirst() 类似的事情。我该如何编写测试来解决这个问题?例如,如果我的两种方法都是错误的,但由于依赖性,测试用例通过了怎么办?
很容易解决。
重命名您的测试(例如添加测试前缀 -
testAddFirst()
)