对间谍对象的调用方法不会对实际的间谍对象产生影响:
public class AAA {
public int a;
public void setA(int aa) {
this.a = aa;
}
public int getA() {
return a;
}
}
public class Proof {
@Test
public void wtf() {
AAA obj = new AAA();
AAA spy = Mockito.spy(obj);
spy.setA(22);
assertThat(obj.getA(), equalTo(22));
}
}
怎么会这样?我想Proof
测试应该通过。
答案 0 :(得分:3)
答案 1 :(得分:2)
我做了一些测试,你应该让spy上的断言不是obj:
@Test
public void wtf() {
AAA obj = new AAA();
AAA spy = Mockito.spy(obj);
spy.setA(22);
assertThat(spy.getA(), equalTo(22));
}