Mockito ArgumentMatchers 不匹配吗?
Mockito ArgumentMatchers doesNotMatch?
ArgumentMatchers.matches( String regex )
存在...并且可以设计 不 匹配给定 String
的正则表达式。但这远非微不足道(SO 中的多个线程)。
我认为请求 Mockito 设计人员承担繁重的工作并将其添加为功能可能是个好主意是我的错误(或错误的想法)吗?看起来,在模拟等等的背景下,它是一个远非例外的用例......
PS 另外,我不清楚 ArgumentMatchers.matches
你怎么说 "this may be a multiline String we're matching against, don't worry about it"... 有一个 Pattern
不是更好吗而不是简单的 String
?
以后
Feature request "enhanced" 在 Mockito 总部(Github)。 "bric3" 据说 "does not match" 应该使用 Jeff Bowman 的技巧。但是 she/he 似乎认为 Pattern
的想法值得考虑。
回复 not()
:Mockito 的 own documentation 说 "Use additional matchers very judiciously because they may impact readability of a test. It is recommended to use matchers from Matchers and keep stubbing and verification simple."
我还发现我必须 "possible dupe" 我自己的问题:How to write a matcher that is not equal to something。事后搜索总是更容易......!
以后还是
非常感谢 Brice 这么快就添加了这个。更新了我的 gradle.build 和...从 Maven Central 下载并立即可用的新 4.1 内核。
无需请求:您可以使用 AdditionalMatchers.not.
编写您想要的内容
when(yourComponent.acceptString(not(matches("foo|ba[rz]"))))
.thenThrow(new IllegalArgumentException());
如果你想匹配一个模式,你可能需要写你自己的 ArgumentMatcher subclass,但从那里开始很容易:
public class MatchesPattern implements ArgumentMatcher<String> {
private final Pattern pattern;
public MatchesPattern(Pattern pattern) { this.pattern = pattern; }
@Override public boolean matches(String string) {
return pattern.matcher(string).matches();
}
@Override public String toString() {
return "[string matching /" + pattern.toString() + "/]";
}
/** Optional. */
public static MatchesPattern matchesPattern(Pattern pattern) {
return new MatchesPattern(pattern);
}
}
然后您可以使用 class 使用:
when(yourComponent.acceptString(not(argThat(new MatchesPattern(yourPattern)))
.thenThrow(new IllegalArgumentException());
// or with the static factory method:
when(yourComponent.acceptString(not(argThat(matchesPattern(yourPattern)))
.thenThrow(new IllegalArgumentException());
对于未来的读者,Mockito 2.4.1 已发布并支持 Pattern
class :
现在你应该可以写了:
when(yourComponent.acceptString(not(matches(Pattern.compile(...)))
.thenThrow(new IllegalArgumentException());
ArgumentMatchers.matches( String regex )
存在...并且可以设计 不 匹配给定 String
的正则表达式。但这远非微不足道(SO 中的多个线程)。
我认为请求 Mockito 设计人员承担繁重的工作并将其添加为功能可能是个好主意是我的错误(或错误的想法)吗?看起来,在模拟等等的背景下,它是一个远非例外的用例......
PS 另外,我不清楚 ArgumentMatchers.matches
你怎么说 "this may be a multiline String we're matching against, don't worry about it"... 有一个 Pattern
不是更好吗而不是简单的 String
?
以后
Feature request "enhanced" 在 Mockito 总部(Github)。 "bric3" 据说 "does not match" 应该使用 Jeff Bowman 的技巧。但是 she/he 似乎认为 Pattern
的想法值得考虑。
回复 not()
:Mockito 的 own documentation 说 "Use additional matchers very judiciously because they may impact readability of a test. It is recommended to use matchers from Matchers and keep stubbing and verification simple."
我还发现我必须 "possible dupe" 我自己的问题:How to write a matcher that is not equal to something。事后搜索总是更容易......!
以后还是
非常感谢 Brice 这么快就添加了这个。更新了我的 gradle.build 和...从 Maven Central 下载并立即可用的新 4.1 内核。
无需请求:您可以使用 AdditionalMatchers.not.
编写您想要的内容when(yourComponent.acceptString(not(matches("foo|ba[rz]"))))
.thenThrow(new IllegalArgumentException());
如果你想匹配一个模式,你可能需要写你自己的 ArgumentMatcher subclass,但从那里开始很容易:
public class MatchesPattern implements ArgumentMatcher<String> {
private final Pattern pattern;
public MatchesPattern(Pattern pattern) { this.pattern = pattern; }
@Override public boolean matches(String string) {
return pattern.matcher(string).matches();
}
@Override public String toString() {
return "[string matching /" + pattern.toString() + "/]";
}
/** Optional. */
public static MatchesPattern matchesPattern(Pattern pattern) {
return new MatchesPattern(pattern);
}
}
然后您可以使用 class 使用:
when(yourComponent.acceptString(not(argThat(new MatchesPattern(yourPattern)))
.thenThrow(new IllegalArgumentException());
// or with the static factory method:
when(yourComponent.acceptString(not(argThat(matchesPattern(yourPattern)))
.thenThrow(new IllegalArgumentException());
对于未来的读者,Mockito 2.4.1 已发布并支持 Pattern
class :
现在你应该可以写了:
when(yourComponent.acceptString(not(matches(Pattern.compile(...)))
.thenThrow(new IllegalArgumentException());