在我自己的代碼中,我最近寫了一個小的Embeddable彈性搜索測試。它將內容存儲在磁盤上,然後可以在使用後刪除文件。我用它來運行我的各種elasticsearch單元測試。
它構成一個單節點elasticsearch集羣。該節點支持完整的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_;
}
要使用它,只需調用getClient,然後你可以使用Elasticsearch的Java API就好了。
來源
2016-02-05 18:38:07
Jon
讓我成爲純粹主義者,但*驗證我的數據實際上已經將它變成ES *不是單元測試,而是* integration *測試,因爲您正在檢查一個組件(代碼)與另一個組件(ES) –