我正在编写一个Junit,以测试对象更改其变量并将其保存到数据库2次的情况。 argumentCaptor
在保存操作中被调用。 getallValues()
返回两个记录。但是两个值都引用到相同的最后一个捕获记录。
ImpplimentationClass.java
...
myobj.setVariable(oneValue);
savemyobj(myobj);
myobj.setVariable(otherValue);
savemyobj(myobj);
...
savemyobj(myobject myobj){
repository.save(myobj);
}
ImpplimentationClassTest.java
private ImplimentationClass underTest ;
@Mock
private Repository mockRepository;
@Before
public void setup(){
initMocks(this);
underTest = new ImplimentationClassTest();
}
@Test
public void test(){
ArgumentCaptor<myobject> captor = ArgumentCaptor.forClass(myobject.class);
myobject obj = new myobject(value);
underTest.implementedMethod(obj);
verify(mockRepository,times(2)).save(captor.capture());
assertEquals(oneValue,captor.getallValues().get(0).getVariable()); //failing here -getting otherValue
assertEquals(otherValue,captor.getallValues().get(1).getVariable());
}
有什么想法可以多次捕获同一对象吗?