In fact, you can do what you need without any additional elasticsearch testing dependencies. The idea is to create an inline node and then use NodeClient to communicate with it.
To do this, I created my own class EmbeddedElasticsearchServer , which looks (more or less) as follows:
public class EmbeddedElasticsearchServer implements InitializingBean { public EmbeddedElasticsearchServer() { ImmutableSettings.Builder elasticsearchSettings = ImmutableSettings.settingsBuilder() .put("http.enabled", "false") .put("path.data", "target/elasticsearch-data"); node = nodeBuilder() .local(true) .settings(elasticsearchSettings.build()) .node(); client = node.client(); } @Override public void afterPropertiesSet() throws Exception {
Then in the spring configuration (let's call it integration-test-context.xml ) I did the following:
<bean id="embeddedElasticsearchServer" class="com.example.EmbeddedElasticsearchServer" /> <bean id="elasticsearchClient" class="org.elasticsearch.client.node.NodeClient" factory-bean="embeddedElasticsearchServer" factory-method="getClient" />
Then you can simply audit the client in your test as follows:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/integration-test-context.xml") public abstract class AbstractElasticsearchIntegrationTest { @Autowired private Client elasticsearchClient;
source share