scala - Elastic4s, mockito and verify with type erasure and implicits -
in previous versions of elastic4s like
val argument1: argumentcapture[deleteindexdefinition] = ??? verify(client).execute(argument1.capture()) assert(argument1 == ???) val argument2: argumentcapture[indexdefinition] = ??? verify(client, times(2)).execute(argument2.capture()) assert(argument2 == ???)
after several executions in test (i.e. 1 deleteindexdefinition
, followed of 2 indexdefinition
). , each verify matched against type.
however, elastic4s takes implicit parameter in client.execute
method. parameter of type executable[t,r]
, means need like
val argument1: argumentcapture[deleteindexdefinition] = ??? verify(client).execute(argument1.capture())(any[executable[deleteindexdefinition,r]]) assert(argument1 == ???) val argument2: argumentcapture[indexdefinition] = ??? verify(client, times(2)).execute(argument2.capture())(any[executable[indexdefinition,r]]) assert(argument2 == ???)
after doing that, getting error. mockito considering both 3 client.execute
in first verify. yes, if first parameter of different type.
that's because implicit(the second parameter) has, after type erasure, same type executable
.
so asertions failing. how test in setup?
the approach taken in elastic4s encapsulate logic executing each request type 1 using typeclasses. why implicit exists. modularize each request type, , avoids god class anti-pattern starting creep elasticclient class.
two things can think of might you:
what posted up, using mockito , passing in implicit matcher. how can mock method using implicits in general.
not use mockito, spool local embedded node, , try against real data. preferred approach when write elasticsearch code. advantages you're testing real queries against real server, not checking invoked, work. (some people might consider integration test, whatever don't agree, runs inside single self contained test no outside deps).
the latest release of elastic4s include testkit makes easy embedded node. can @ of unit tests give idea how use it.
Comments
Post a Comment