首页 > 解决方案 > 如何使用when...then模拟层次结构吸气剂

问题描述

我想模拟一行代码

List<Status> Status = xyz.stream().filter(Predicate).collect(Collectors.toList());

我尝试使用模拟

但它在 Mockito 中抛出空指针。如何模拟这一行,使我的列表不为空并返回我通过 thenReturn 传递的内容。

标签: junitmockito

解决方案


您需要模拟链中的每个部分。

Ung mockedUng = Mockito.mock(Ung.class);
when(mockedUng.getFguBeskaeftigelseStatus()).thenReturn(fGUBeskaeftigelsesStatusList);
when(mockforloeb.getUng()).thenReturn(mockedUng);

推荐阅读