我试图用Mockito模拟一个类,它有一个链接接口的方法.该类被成功模拟,但是当它调用接口时,抛出一个空指针.代码如下所示:
mock = Mockito.mock(MyProcess.class);
process = mock.getProcess()
.getService() //Interface throwing null exception
.startProcessInstanceByKey("String argument");
Run Code Online (Sandbox Code Playgroud)
我得到了这个解决方案,并尝试按照下面的页面上的示例进行操作,但它不起作用:https://static.javadoc.io/org.mockito/mockito-core/2.13.0/org/mockito/Mockito.html#RETURNS_DEEP_STUBS
Foo mock = mock(Foo.class, RETURNS_DEEP_STUBS);
// note that we're stubbing a chain of methods here: getBar().getName()
when(mock.getBar().getName()).thenReturn("deep");
// note that we're chaining method calls: getBar().getName()
assertEquals("deep", mock.getBar().getName());
Run Code Online (Sandbox Code Playgroud)
上面的例子不起作用
显然我不擅长 Java 8。有人能告诉我为什么我的集合在迭代过程中没有保留我的所有值吗?如果我放置一个断点,代码会迭代但只返回最后一个值。
Set<Group> groups = new Hashset<>();
Group group = new Group();
List my = model.getValues();
list.stream().forEach(gr -> {
group.setName(gr.getDescription());
group.setDescription(gr.getDescription());
groups.add(group);
});
System.Out.Println(groups);
Run Code Online (Sandbox Code Playgroud)