- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试组成两个匹配器,这样就不用写了
assertThat(response, hasStatusCode(OK));
assertThat(response, hasMessage("Some message."));
我可以写一些类似的东西
assertThat(response,
both(hasStatusCode(OK))
.and(hasMessage("Some message.")));
但是,当一个或两个匹配器失败时运行断言时,我会得到不需要的奇怪输出:
Expected: (status code to be <200> and response to contain message "Some Message.")
but: response to contain message "Some Message." response message was "Actual message"
似乎有什么东西干扰了不匹配的文本。我希望“但是”行读起来像 但是:(状态代码为<200>,响应消息为“实际消息”)
从逻辑上讲,匹配器似乎工作正常。
匹配器是:
private Matcher<Response> hasMessage(final String expectedMessage) {
return new TypeSafeDiagnosingMatcher<Response>() {
@Override
protected boolean matchesSafely(final Response response, final Description mismatch) {
String message = response.getEntity().toString();
if (message != expectedMessage) {
mismatch.appendText("response message was ").appendValue(message);
return false;
}
return true;
}
@Override
public void describeTo(final Description description) {
description.appendText("response to contain message ").appendValue(expectedMessage);
}
};
}
private Matcher<Response> hasStatusCode(final Status expectedStatusCode) {
return new TypeSafeDiagnosingMatcher<Response>() {
@Override
protected boolean matchesSafely(final Response response, final Description mismatch) {
int statusCode = response.getStatus();
if (expectedStatusCode.getStatusCode() != statusCode) {
mismatch.appendText("status code was ").appendValue(statusCode);
}
return true;
}
@Override
public void describeTo(final Description description) {
description.appendText("status code to be ").appendValue(expectedStatusCode.getStatusCode());
}
};
}
最佳答案
这就是 AllOf
呈现不匹配的方式:它仅显示第一个失败的 Matcher
,其中 its description followed by its description of the mismatch :
if (!matcher.matches(o)) {
mismatch.appendDescriptionOf(matcher).appendText(" ");
matcher.describeMismatch(o, mismatch);
return false;
}
考虑到结果的生成方式,在不匹配消息中添加额外的语言(例如“不匹配,因为”)可能会让事情变得更清楚。 open pull request 建议 Hamcrest 可以使描述更清晰,但也有不明确的边缘情况。
请参阅 Strange AllOf hamcrest matcher mismatch description 了解类似的问题,涉及现有匹配器而不是自定义匹配器。
关于java - 编写与 Hamcrest AllOf/CombinableMatcher 匹配器配合良好的自定义匹配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24762380/
嗨,我正在考虑开发一种文件传输程序,想知道我是否想要尽可能好的加密,我应该使用什么? 我会用 C# 开发它,所以我可以访问 .net 库 :P在我的 usb 上有一个证书来访问服务器是没有问题的,如果
我创建的这个计算两个数组的交集是线性的方法的复杂度(在良好、平均、最差的情况下)? O(n) public void getInt(int[] a,int[] b){ int i=0; int
我已经能够使用 RTCPeerConnection.getStats() API 获得 WebRTC 音频调用的各种统计信息(抖动、RTT、丢包等)。 我需要将整体通话质量评为优秀、良好、一般或差。
基本问题: 如果我正在讲述/修改数据,我应该通过索引硬编码索引访问文件的元素,即 targetFile.getElement(5);通过硬编码标识符(内部翻译成索引),即 target.getElem
在 Linux 上,我想知道要调用什么“C”API 来获取每个 CPU 的统计信息。 我知道并且可以从我的应用程序中读取 /proc/loadavg,但这是系统范围的负载平均值,而不是每个 CPU 的
在客户端浏览器中使用 fetch api,GET 或 POST 没有问题,但 fetch 和 DELETE 有问题。它似乎将 DELETE 请求方法更改为 OPTIONS。 大多数研究表明是一个cor
我是一名优秀的程序员,十分优秀!