I'm using elastic4s as my access layer to ElasticSearch, and I'm trying to write some unit tests in my application. I'm using scalaMock as my mocking library. I want to mock the elastic4s client.execute function so I will be able to test my code.
this is the tested code:
def insert(elasticDbConnection: ElasticClient, entIndexName: String, entTypingName: String, autoId: String, newJsonEntVal: String): Option[List[String]] = {
import com.sksamuel.elastic4s.http.ElasticDsl._
val req: IndexRequest = indexInto(index = entIndexName, `type` = entTypingName)
.id(id = autoId).source(newJsonEntVal).refreshImmediately
val insertRequests: List[IndexRequest] = List(req)
val execRes: Future[Response[BulkResponse]] = elasticDbConnection.execute(bulk(insertRequests))
val insertRes: BulkResponse = execRes.await.result
val insertedEnts = insertRes.successes.map(resItem => resItem.id).toList
Some(insertedEnts)
}
now the unit test code is:
...
import com.sksamuel.elastic4s.http.ElasticDsl._
val execRes: Future[Response[BulkResponse]]= mock[Future[Response[BulkResponse]]]
val elasticClientMock = mock[ElasticClient]
(elasticClientMock.execute _).expects(_:BulkRequest).returns(execRes).once()
testElasticsDal.insert(elasticClientMock, "indexName", "entType", "test-id", "{testField:\"testValue\"}")
I'm getting an error : "Type mismatch, expected: FunctionAdapter1[BulkRequest, Boolean], actual: BulkRequest Type mismatch, expected: MockParameter[BulkRequest], actual: BulkRequest"
What am I doing wrong? how should I test my application code? and how should I mock the client?
Thank you