[英]FlatExtracting Map with AssertJ 3
I'm using AssertJ 3.11.1 and trying to get rid of Hamcrest completely. 我正在使用AssertJ 3.11.1,并试图完全摆脱Hamcrest。 I managed so far but I'm having trouble with the following code:
到目前为止,我已经设法完成了,但是遇到了以下代码的麻烦:
class Foo {
private final Map<String, String> innerMap;
Foo(Map<String, String> innerMap) {
this.innerMap = innerMap;
}
public Map<String, String> getInnerMap() {
return this.innerMap;
}
}
Map<String, String> innerMap = new HashMap<>();
innerMap.put("key1", "value1");
innerMap.put("key2", "value2");
Foo foo = new Foo(innerMap);
Map<Foo, String> map = new HashMap<>();
map.put(foo, "notUsed");
With Hamcrest, I can easily do: 使用Hamcrest,我可以轻松做到:
assertThat(metrics,
hasKey(
hasProperty("innerMap",
allOf(hasEntry("key1", "value1"), hasEntry("key2", "value2")))));
However, there's no easy translation to AssertJ. 但是,没有简单的翻译成AssertJ。 My best approach so far has been something like this (Java 8):
到目前为止,我最好的方法是这样的(Java 8):
assertThat(metrics.keySet().stream().map(Foo::getInnerMap))
.flatExtracting(Map::entrySet)
.contains(entry("key1", "value1"), entry("key2", "value2"));
But this code is not nearly as descriptive as with Hamcrest not to mention that I get an Unchecked generic array creation for varargs parameter
compilation warning. 但是,这段代码的描述性不如Hamcrest那样,更不用说我得到了
Unchecked generic array creation for varargs parameter
编译警告的Unchecked generic array creation for varargs parameter
。
Is there a better way to do it? 有更好的方法吗? I'm willing to implement my own custom assertion and perhaps use
hasKeySatisfying()
from AbstractMapAssert
if that's necessary for a cleaner code. 我愿意实现自己的自定义断言,也许使用
hasKeySatisfying()
从AbstractMapAssert
如果对于一个更干净的代码是必要的。
I know that I can use HamcrestCondition
but I would rather not use Hamcrest at all. 我知道我可以使用
HamcrestCondition
但我完全不希望使用Hamcrest。
Thanks in advance! 提前致谢!
EDIT: 编辑:
For now, I came up with this: 现在,我想到了这个:
public class ExtendedMapAssert<K, V> extends MapAssert<K, V> {
ExtendedMapAssert(final Map<K, V> actual) {
super(actual);
}
public ExtendedMapAssert<K, V> hasAnyKeySatisfying(final Consumer<K> consumer) {
isNotNull();
assertThat(actual.keySet()).isNotEmpty().anySatisfy(consumer);
return this;
}
}
public static <K, V> ExtendedMapAssert<K, V> assertThatMap(final Map<K, V> map) {
return new ExtendedMapAssert<>(map);
}
And then use it like: 然后像这样使用它:
assertThatMap(metrics)
.hasAnyKeySatisfying(
metricId ->
assertThat(metricId.getTags())
.contains(entry("key1", "value1"), entry("key2", "value2")));
Any other ideas? 还有其他想法吗?
You could try hasKeySatisfying
and build a Condition
with a lambda (there is no similar assertion that would take a Consumer
yet but contributions are welcomed!) or take an approach you suggested but in a simpler way: 您可以尝试使用
hasKeySatisfying
并使用lambda构建Condition
(没有类似的断言会占用Consumer
,但欢迎您提供帮助!),也可以采用您建议的方法,但方法更简单:
// assertion fails for empty collections no need to use isNotEmpty
assertThat(metrics.keySet()).anySatisfy(metricId ->
assertThat(metricId.getTags()).contains(entry("key1", "value1"), entry("key2", "value2")));
Hope it helps ... 希望能帮助到你 ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.