英文:
Mockito testing for function with 2 args
问题
我有以下的流:
Collection<FieldsMapperMetadata> fieldsMapperMetadataCollection =
Optional.ofNullable(typesMapperDefinition.getFieldMappersDefinitions()).orElse(Collections.emptyList())
.stream()
.map(fieldsMapperDefinition -> fieldsMapperMetadataFactory.apply(typesMapperDefinition, fieldsMapperDefinition))
.collect(Collectors.toList());
我正试图使用 Mockito 进行测试,但在这部分遇到了问题:
fieldsMapperDefinition -> fieldsMapperMetadataFactory.apply(typesMapperDefinition, fieldsMapperDefinition)
这是函数的签名:
private BiFunction<TypesMapperDefinition, FieldsMapperDefinition, FieldsMapperMetadata> fieldsMapperMetadataFactory;
我尝试做类似于以下的事情:
Mockito.when(fieldsMapperMetadataFactory.apply(Mockito.eq(typesMapperDefinition1,fieldsMapperDefinition1)).thenReturn(fieldsMapperMetadata1);
但是似乎 Mockito.eq 不是正确的方法...
对此的任何帮助将不胜感激
英文:
I have the following stream:
Collection<FieldsMapperMetadata> fieldsMapperMetadataCollection =
Optional.ofNullable(typesMapperDefinition.getFieldMappersDefinitions()).orElse(Collections.emptyList())
.stream()
.map(fieldsMapperDefinition -> fieldsMapperMetadataFactory.apply(typesMapperDefinition, fieldsMapperDefinition))
.collect(Collectors.toList());
And I'm trying to test it using mockito, but I'm having a problem with this part
fieldsMapperDefinition -> fieldsMapperMetadataFactory.apply(typesMapperDefinition, fieldsMapperDefinition)
here is the function signature
private BiFunction<TypesMapperDefinition, FieldsMapperDefinition, FieldsMapperMetadata> fieldsMapperMetadataFactory;
and I was trying to do something like this:
Mockito.when(fieldsMapperMetadataFactory.apply(Mockito.eq(typesMapperDefinition1,fieldsMapperDefinition1)).thenReturn(fieldsMapperMetadata1);
but seems like the Mockito.eq isn't the right approach...
Any help on this would be appriciated
答案1
得分: 1
你只向 apply 方法传递了一个参数,你应该调用 eq()
两次:
Mockito.when(fieldsMapperMetadataFactory
.apply(Mockito.eq(typesMapperDefinition1), Mockito.eq(fieldsMapperDefinition1))
.thenReturn(fieldsMapperMetadata1);
英文:
You pass only one argument to the apply method, you should call eq()
twice:
Mockito.when(fieldsMapperMetadataFactory
.apply(Mockito.eq(typesMapperDefinition1), Mockito.eq(fieldsMapperDefinition1))
.thenReturn(fieldsMapperMetadata1);
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论