我有一個我試圖模擬的函式,它包含 while 回圈形式的遞回邏輯,我試圖弄清楚如何訪問 while 回圈的內部而不是永遠回圈。
//this is supposed to go through all the items in the grocery list given the parameters until the groceries are all checked out
public void checkOut(String maxItems, String code){
List<cereal> groceryList;
groceryList = groceryListDao.getList(String maxItems, String code);
while (!groceryList.isEmpty()){
groceryListDao.total();
//other logic
groceryList = groceryListDao.getList(String maxItems, String code);
}
}
我能夠撰寫一個 junit 測驗來驗證如果購物清單為空,則永遠不會進入 while 回圈。但是,我不確定如何撰寫代碼來測驗是否進入了 while 回圈,因為我需要模擬雜貨清單 Dao.getList 不為空以進入 while 回圈,然后為空以退出 while 回圈。我不知道如何做到這兩點。
@Test
public void checkout() {
List<Cereal> cereal = new ArrayList<>();
Cereal x = new Cereal();
cereal.add(x);
when(groceryListDao.getList(anyString(), anyString())).thenReturn(cereal);
groceryService.checkout("10", "A5ALV350IIXL");
verify(groceryListDao, times(1).total());
}
如何驗證是否在回圈內呼叫了 total() 而不會卡住?
uj5u.com熱心網友回復:
您可以鏈接thenReturn,以便隨后對模擬的呼叫回傳不同的內容:
public class GroceryServiceTest {
@Test
public void checkout() {
GroceryService.GroceryListDao groceryListDao = mock(GroceryService.GroceryListDao.class);
GroceryService groceryService = new GroceryService(groceryListDao);
List<GroceryService.Cereal> cereal = new ArrayList<>();
GroceryService.Cereal x = new GroceryService.Cereal();
cereal.add(x);
// first return a list with one item, then an empty list
when(groceryListDao.getList(anyString(), anyString())).thenReturn(cereal).thenReturn(Collections.emptyList());
groceryService.checkout("10", "A5ALV350IIXL");
verify(groceryListDao, times(1)).total();
}
}
這不是一個完美的測驗,因為模擬將回傳一個空串列,而沒有對total.
您可以像這樣模擬 DAO 的語意:
public class GroceryServiceTest {
@Test
public void checkout() {
GroceryService.GroceryListDao groceryListDao = mock(GroceryService.GroceryListDao.class);
GroceryService groceryService = new GroceryService(groceryListDao);
List<GroceryService.Cereal> cereal = new ArrayList<>();
AtomicBoolean totalCalled = new AtomicBoolean(false);
GroceryService.Cereal x = new GroceryService.Cereal();
cereal.add(x);
when(groceryListDao.getList(anyString(), anyString())).thenAnswer(new Answer<List<GroceryService.Cereal>>() {
@Override
public List<GroceryService.Cereal> answer(InvocationOnMock invocationOnMock) throws Throwable {
if (totalCalled.get()) {
return Collections.emptyList();
} else {
return cereal;
}
}
});
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocationOnMock) throws Throwable {
totalCalled.set(true);
return null;
}
}).when(groceryListDao).total();
groceryService.checkout("10", "A5ALV350IIXL");
verify(groceryListDao, times(1)).total();
}
}
為了完整起見,這里是GroceryService代碼:
import java.util.List;
public class GroceryService {
final GroceryService.GroceryListDao groceryListDao;
public GroceryService(GroceryService.GroceryListDao groceryListDao) {
this.groceryListDao = groceryListDao;
}
interface GroceryListDao {
List<Cereal> getList(String maxItems, String code);
void total();
}
static class Cereal {}
public void checkout(String maxItems, String code){
List<Cereal> groceryList;
groceryList = groceryListDao.getList(maxItems, code);
while (!groceryList.isEmpty()){
groceryListDao.total();
//other logic
groceryList = groceryListDao.getList(maxItems, code);
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/330768.html
