I am writing Junit test case(Junit 5 with jupitar) for existing code that has while loop with break statement.
public int deleteRecords(int batchsize) {
//some code here
int totalDeletedRecords = 0;
while(true) {
int deleted = deleteRecord();
if (deleted == 0) { //means no record found to delete
break;
}
totalDeletedRecords += deleted;
}
}
private int deleteRecord() {
List<User> usersList = repository.findAll(); //get the records from the database;
if (CollectUtils.isEmpty()) {
return 0;
}
//then deleting the total records batch wise
//construct list with ids and pass that to delete method
repository.delete(List of ids);
return userList.size();
}
My Junit测试案
@Test
public void testDeleteRecordSuccess() {
//mock repository call
User user = User.builder().name("xyz").build();
when(repository.findAll()).thenReturn(Arrays.asList(user));
doNothing().when(repository).delete(anyList());
int deleted = service.deleteRecords(1000);
assertEquals(1, deleted);
}
按照模拟计算,记录总有1个大小, lo只执行。 休息需要停止,而且所有法典应涵盖范围。
让我知道,如何在这种情景中改变存放处的要求。