1

I've been having some trouble making some unit tests for old code from another company. I think I almost got them working but I'm getting an AbstractMethodError when I run the test.

The code snippet that is failing is the following:

        val onSuccess = slot<(List<AEQuery>) -> Unit>()
        val onFailure = slot<(Failure) -> Unit>()
        coEvery { getAEQueries(GetAEQueries.Params.forAEQueryRequest(queryRequest)).fold(capture(onFailure), capture(onSuccess)) } coAnswers {
            onSuccess.captured.invoke(expectedQueries)
        }

As you can see, I need to use slot to capture the lambdas that the fold() function is taking as parameters, but I'm getting this error:

Receiver class kotlin.jvm.functions.Function1$Subclass0 does not define or inherit an implementation of the resolved method 'abstract java.lang.Object invoke(java.lang.Object)' of interface kotlin.jvm.functions.Function1.
java.lang.AbstractMethodError: Receiver class kotlin.jvm.functions.Function1$Subclass0 does not define or inherit an implementation of the resolved method 'abstract java.lang.Object invoke(java.lang.Object)' of interface kotlin.jvm.functions.Function1.

I've checked all links with the same problems and nothing helped. I'm a bit desperate and would appreciate any help. Thanks

1 Answers1

0

The receiver of fold() needs to be a mock and will throw this exception if not, since it otherwise executes the actual implementation and tries to invoke the lambda arguments passed (e.g. capture(onSuccess)).

Assuming getAEQueries() is a top-level function, it needs to be mocked.

nick18702
  • 54
  • 6