How to make Unit test / mock ElasticSearch

Firstly, I use Scala and sbt for my application.

I use the ElasticClient library of elastic libraries to connect to my ES instance. So basically I just have to be able to test them in my unit tests. For example, just checking that my data actually turned it into ES and the like.

Would you taunt ElasticSearch in the best way, or is there a better way to do this? And how would I get around them?

I found that you can configure the local client with ElasticClient.local, but I can not find many examples. We would like to go with this implementation, so if you know how to use it, I would like to hear about it, but if there are better or easier ways to complete this, it will work.

+6
source share
3 answers

since elastic search is java and your code is too (or compatible), the best approach would be to determine how to load elastic search "embeddable" - just start your server in your @Before method and close it / clear in @After .

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

+2
source

For our ElasticSearch tests, we use a permanent instance of ElasticSearch on our Jenkins build server, which uses each test. For local tests, you need to run local ElasticSearch. We use the rest interface, not the java api.

To make unit tests parallel, we use a global, synchronized name pool (for index names). Each test can configure the json index definition, and if it works fine on a dirty (= already filled) index. A small test superclass (scalatest) will get the index name from the pool. If a clean index is required, (possibly) the existing one will be deleted, and a new one will be created. If the test accepts a dirty index, it checks to see if the definition of the index matches the configured one. If not, the index will also be recreated.

Depending on your test cases, this allows you to navigate with multiple indexes that will be recreated once at a time, and are often reused with tests, speeding up the test.

+1
source

In my own code, I recently wrote a small plugin elastic search for testing. It stores things on disk, and then can delete files after use. I use this to run my various elasticsearch unit test.

It creates a single cluster of cluster nodes. This node supports the full elasticsearch API.

  /** * A simple embeddable Elasticsearch server. This is great for integration testing and also * stand alone tests. * * Starts up a single ElasticSearch node and client. */ public class EmbeddedElasticsearchServer { public EmbeddedElasticsearchServer(String storagePath) { storagePath_ = storagePath; ImmutableSettings.Builder elasticsearchSettings = ImmutableSettings.settingsBuilder() .put("http.enabled", "false") .put("path.data", storagePath_); node_ = new NodeBuilder() .local(true) .settings(elasticsearchSettings.build()) .node(); client_ = node_.client(); } public Client getClient() { return client_; } public void shutdown() { node_.close(); } public void deleteStorage() throws IOException { File storage = new File(storagePath_); if(storage.exists()) { FileUtils.deleteDirectory(storage); } } private Client client_; private Node node_; private String storagePath_; } 

To use it, just call getClient, and then you can just use the Java Elasticsearch API.

+1
source

All Articles