0

Consider DynamoDB's QueryApi. Through a series of (unfortunate?) hoops,

ItemCollection<QueryOutcome>>

ends up being equivalent to

Iterable<Item>

I know this because I can do:

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());
}

But when I try to write tests in Mockito using BDDMockito:

@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);
}

The compiler complains when I try to make items an Iterable.

Why dis?

Community
  • 1
  • 1

1 Answers1

3

Dis not because of BDDMockito. Dis because ItemCollection<QueryOutcome> simply can't be cast safely into Iterable<Item>. It can be cast into Iterable<QueryOutcome> or even Iterable<? extends Item>, but not Iterable<Item>.

Otherwise you could do this:

final ItemCollection<QueryOutcome> items = mock(ItemCollection.class);
Collection<Item> yourItemCollection = items;
yourItemCollection.add(itemThatIsNotAQueryOutcome);  // violating safety of items

See also:

Community
  • 1
  • 1
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251