Mock elasticsearch with mock-server
In a Apache Spark Java project, that must send data to elasticsearch with the official plugin elasticsearch-hadoop (http://github.com/elastic/elasticsearch-hadoop/) I use junit to perfom unit-test.
Since elasticsearch represents less than 5% of data in the project, I don’t want to launch a real ES server but mock it, so we can perform HTTP queries without trouble.
http://www.mock-server.com/ is a very simple Java library that use Netty to create a real HTTP server where you can add rules (query and response).
First, we must define what elasticsearch services we want mock:
- “/” => give version
- “*/_search” => give documents
- “*/_count” => give count
- etc..
Here an example:
Of course, this is a basic example, this does nothing. You can modify it to fit your test workflow and indices/data.