实际上,您可以执行所需的 *** 作而无需任何其他的Elasticsearch测试依赖项。这个想法基本上是创建一个嵌入式节点,然后使用
NodeClient与之通信。
为此,我创建了自己的
EmbeddedElasticsearchServer类(或多或少)如下:
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 { // Initialization stuff: // - create required indices // - define mappings // - populate with test data } public Client getClient() { return client; }}
然后,在spring配置中(我们称之为
integration-test-context.xml),我这样做:
<bean id="embeddedElasticsearchServer" /><bean id="elasticsearchClient" factory-bean="embeddedElasticsearchServer" factory-method="getClient" />
然后,您可以像这样在测试中自动连接客户端:
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("/integration-test-context.xml")public abstract class AbstractElasticsearchIntegrationTest { @Autowired private Client elasticsearchClient; // Your rests go here...}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)