2017-04-03 105 views
0

考慮DynamoDB's QueryApi。通過對爲什麼在這種情況下BDDMockito不能解析類型?

Iterable<Item> 

一系列的(?不幸的)箍,

ItemCollection<QueryOutcome>> 

最後等於我知道這是因爲我可以做的:

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

但是,當我嘗試寫在Mockito中使用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); 
} 

當我嘗試使itemsIterable時,編譯器發出抱怨。

爲什麼dis?

回答

1

不是因爲BDDMockito。因爲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 

參見:

相關問題