简体   繁体   中英

Mockito testing for function with 2 args

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM