Mockito测试具有2个参数的函数

huangapple go评论71阅读模式
英文:

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&lt;FieldsMapperMetadata&gt; fieldsMapperMetadataCollection =
            Optional.ofNullable(typesMapperDefinition.getFieldMappersDefinitions()).orElse(Collections.emptyList())
                    .stream()
                    .map(fieldsMapperDefinition -&gt; 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 -&gt; fieldsMapperMetadataFactory.apply(typesMapperDefinition, fieldsMapperDefinition)

here is the function signature

private BiFunction&lt;TypesMapperDefinition, FieldsMapperDefinition, FieldsMapperMetadata&gt; 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>



huangapple
  • 本文由 发表于 2020年10月2日 23:13:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64174026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定