jeudi 30 juillet 2020

Mockito matcher for lambda parameters

I have a method with lambda parameter in Kotlin like this:

interface IQueryBuilder {
    ...
    fun filter(filter: IFilterCondition.() -> Unit): IQueryBuilder
}

I am trying to mock this method with 'anyOrNull()' matcher from mockito-kotlin to return itself instead of null. But it doesn't find my matcher and returns null. How should I mock lambda expressions?

Mocking code:

whenever(queryBuilder.filter(anyOrNull<IFilterCondition.() -> Unit>())).thenReturn(queryBuilder)

Function call in the code:

queryBuilder.filter {
    "Id" eq 1
    "Name" eq "Test"
}

!! Moving lamda inside of parentheses doesn't change anything. Android Studio warns about to use this syntax.

Aucun commentaire:

Enregistrer un commentaire