我如何在同一个Mock上获得两个方法调用以返回不同的值?



我有一个函数,我试图模拟包含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);
}
}

我可以编写一个单元测试来验证,如果购物列表为空,则永远不会进入while循环。但是,我不确定如何编写代码来测试while循环是否进入,因为我需要模拟groceryListDao。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()在循环内被调用而不会卡住?

您可以链接thenReturn,以便对mock的后续调用返回不同的内容:

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();
}
}

这不是一个完美的测试,因为mock将返回一个空列表,而不需要调用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);
}
}
}

相关内容

  • 没有找到相关文章

最新更新