考虑DynamoDB's QueryApi
。通过一系列(不幸的?)箍,
ItemCollection<QueryOutcome>>
最终等同于
Iterable<Item>
我知道这是因为我能做到:
public PuppyDog getPuppy(final String personGuid, final String name) {
final QuerySpec spec = new QuerySpec()
.withKeyConditionExpression("#d = :guid and #n = :name")
.withNameMap(new NameMap().with("#d", "guid").with("#n", "name"))
.withValueMap(new ValueMap().withString(":guid", personGuid).withString(":name", name));
return getDog(index.query(spec));
}
private PuppyDog getDog(final Iterable<Item> itemCollection) {
// http://stackoverflow.com/questions/23932061/convert-iterable-to-stream-using-java-8-jdk
return StreamSupport.stream(itemCollection.spliterator(), false)
.map(this::createDogFor)
// it would be a little weird to find more than 1, but not sure what to do if so.
.findAny().orElse(new PuppyDog());
}
但是当我尝试使用BDDMockito在Mockito中编写测试时:
@Test
public void canGetPuppyDogByPersonGuidAndName() {
final PuppyDog dawg = getPuppyDog();
final ArgumentCaptor<QuerySpec> captor = ArgumentCaptor.forClass(QuerySpec.class);
final ItemCollection<QueryOutcome> items = mock(ItemCollection.class);
given(query.query(captor.capture())).willReturn(items);
}
当我尝试items
Iterable
时,编译器会抱怨。
为何选择?
答案 0 :(得分:3)
不要因为BDDMockito。 Dis因为ItemCollection<QueryOutcome>
根本无法安全地投入
Iterable<Item>
。它可以转换为Iterable<QueryOutcome>
甚至Iterable<? extends Item>
,但不能转换为Iterable<Item>
。
否则你可以这样做:
final ItemCollection<QueryOutcome> items = mock(ItemCollection.class);
Collection<Item> yourItemCollection = items;
yourItemCollection.add(itemThatIsNotAQueryOutcome); // violating safety of items
另见: