Skip to content

Instantly share code, notes, and snippets.

@tamast
Created January 11, 2017 18:43
Show Gist options
  • Save tamast/37424c443a13d73c698fef8a48e65536 to your computer and use it in GitHub Desktop.
Save tamast/37424c443a13d73c698fef8a48e65536 to your computer and use it in GitHub Desktop.
Elasticsearch Embedded test
// use this dependency in your pom.xml
// <dependency>
// <groupId>org.elasticsearch</groupId>
// <artifactId>elasticsearch</artifactId>
// <version>2.4.0</version>
// </dependency>
@Test
public void elasticsearchForTests() throws InterruptedException
{
Settings.Builder settingsBuilder =
Settings.settingsBuilder();
settingsBuilder.put("node.name", "test"); //Creates files, should be removed after test
settingsBuilder.put("path.home", "test");
settingsBuilder.put("path.data", "test");
settingsBuilder.put("http.enabled", true); //ES is available via HTTP
Settings settings = settingsBuilder.build();
NodeBuilder.nodeBuilder()
.settings(settings)
.clusterName("test")
.data(true).local(true).node();
Thread.sleep(1000); //have to wait for elastic start
//Call it
RestTemplate restTemplate = new RestTemplate();
try {
Movie movie = restTemplate.getForObject("http://localhost:9200/movies/movie/1", Movie.class);
}
catch(HttpClientErrorException e)
{
Assert.assertTrue(StringUtils.startsWithIgnoreCase(e.getResponseBodyAsString(),"{\"error\":{\"root_cause\":[{\"type\":\"index_not_found_exception\""));
System.out.println("Response from ES: " + e.getResponseBodyAsString());
}
}
public static class Movie
{
private String error;
}
@tamast
Copy link
Author

tamast commented Jan 11, 2017

settingsBuilder.put("index.store.type", "memory")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment