Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Unit test/mock ElasticSearch

First off I'm using Scala and sbt for my application.

I'm using the elastic4s library's ElasticClient to connect to my ES instance. So basically I just need to be able to test these in my unit tests. Such as just verifying that my data actually made it into ES and things like that.

Would mocking ElasticSearch be the best way to go or is there a more efficient way of doing this? And how would I go about either of those?

I've found that you can set up a local client with ElasticClient.local but I can't seem to find many examples. We would like to go with this implementation so if you know how to use this I'd like to hear about it but if there are better or easier ways to complete this that would work.

like image 440
Ryan Wilson Avatar asked Dec 07 '15 19:12

Ryan Wilson


1 Answers

since elastic search is java and your code is too (or compatible) the best approach would be figure out a way to boot elastic search "embeddable" - just start their server up in your @Before method and shut it down / clear it in @After.

luckily for you, it seems like someone already had the exact same idea - https://orrsella.com/2014/10/28/embedded-elasticsearch-server-for-scala-integration-tests/

like image 179
radai Avatar answered Sep 30 '22 16:09

radai