我在函数中有一行代码,它对 as 应用流操作ArrayList:
List<Entity> list = xyz();
long result = list.stream().map(n -> n.getData()).filter(n -> n == 1).count();
Run Code Online (Sandbox Code Playgroud)
现在,当我为此方法编写测试时,我应该这样做:
@Mock
private List<Entity> list;
//inside the test method
when(list.stream().map(any()).filter(any()).count()).thenReturn(someValue);
Run Code Online (Sandbox Code Playgroud)
我想到的是,当我们在代码中调用流操作时,我们基本上是走出类来调用这些函数。由于这是单元测试,我们应该留在我们的模块内。如果我有一些误解,请澄清。如果我们不需要嘲笑List,那为什么呢?