我正在寻找关于lambda的信息,虽然我无法找到类似于下面功能的东西。它属于org.springframework.test.web.servlet.result.JsonPathResultMatchers类,ResultMatcher是@FunctionalInterface,结果是MvcResult和jsonPathHelper.doesNotExist类型的返回空
public ResultMatcher doesNotExist() {
return result -> jsonPathHelper.doesNotExist(getContent(result));
}我打电话到上面
jsonPath("$._embedded" ).doesNotExist()我完全不知道:
谢谢
发布于 2019-01-08 00:37:32
代码中的lambda:
result -> jsonPathHelper.doesNotExist(getContent(result));只是ResultMatcher的一个表示形式,因为它是一个FunctionalInterface。你可以把它看作是:
public ResultMatcher doesNotExist() {
return new ResultMatcher() {
@Override
public void match(MvcResult result) throws Exception {
jsonPathHelper.doesNotExist(getContent(result)); // returns void
}
};
}如果jsonPathHelper.doesNotExist返回空,那么为什么doesNotExist返回ResultMatcher
您的方法doesNotExist,只返回函数接口本身,然后可以用来调用它的match函数。注意,调用也将返回void。
类具有类似于结果的任何内容,这个论点从何而来?
如果您查看上面的匿名类,使用lambda表示,result将成为match方法在ResultMatcher实现中的参数。
因此,当您实际希望访问此实现(或一般情况下的ResultMatcher )时,将按以下方式调用该方法(简化初始化):
ResultMatcher resultMatcher = doesNotExist(); // your method returns here
MvcResult result = new MvcResult(); // some MvcResult object
resultMatcher.match(result); // actual invocationhttps://stackoverflow.com/questions/54083561
复制相似问题