如何在Java中为Elasticsearch编写适当的单元测试
概述: 我是弹性搜索测试的新手,我将添加适当的单元测试。项目兼容性如下所示:如何在Java中为Elasticsearch编写适当的单元测试,java,unit-testing,spring-boot,
elasticsearch,junit,Java,Unit Testing,Spring Boot,
elasticsearch,Junit,概述: 我是弹性搜索测试的新手,我将添加适当的单元测试。项目兼容性如下所示: import static java.net.InetAddress.getByName; import static java.util.Arrays.stream; import java.net.UnknownHostException; import java.util.Map; import java.util.Objects; import javax.inject.Inject; import or
import static java.net.InetAddress.getByName;
import static java.util.Arrays.stream;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.Objects;
import javax.inject.Inject;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import au.com.api.util.RestClientUtil;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Configuration
public class ElasticConfiguration implements InitializingBean{
@Value(value = "${elasticsearch.hosts}")
private String[] hosts;
@Value(value = "${elasticsearch.httpPort}")
private int httpPort;
@Value(value = "${elasticsearch.tcpPort}")
private int tcpPort;
@Value(value = "${elasticsearch.clusterName}")
private String clusterName;
@Inject
private RestClientUtil client;
@Bean
public RestHighLevelClient restHighClient() {
return new RestHighLevelClient(RestClient.builder(httpHosts()));
}
@Bean
@Deprecated
public RestClient restClient() {
return RestClient.builder(httpHosts()).build();
}
/**
* @return TransportClient
* @throws UnknownHostException
*/
@SuppressWarnings("resource")
@Bean
public TransportClient transportClient() throws UnknownHostException{
Settings settings = Settings.builder()
.put("cluster.name", clusterName).build();
return new PreBuiltTransportClient(settings).addTransportAddresses(transportAddresses());
}
@Override
public void afterPropertiesSet() throws Exception {
log.debug("loading search templates...");
try {
for (Map.Entry<String, String> entry : Constants.SEARCH_TEMPLATE_MAP.entrySet()) {
client.putInlineSearchTemplateToElasticsearch(entry.getKey(), entry.getValue());
}
} catch (Exception e) {
log.error("Exception has occurred in putting search templates into ES.", e);
}
}
private HttpHost[] httpHosts() {
return stream(hosts).map(h -> new HttpHost(h, httpPort, "http")).toArray(HttpHost[]::new);
}
private TransportAddress[] transportAddresses() throws UnknownHostException {
TransportAddress[] transportAddresses = stream(hosts).map(h -> {
try {
return new TransportAddress(getByName(h), tcpPort);
} catch (UnknownHostException e) {
log.error("Exception has occurred in creating ES TransportAddress. host: '{}', tcpPort: '{}'", h, tcpPort, e);
}
return null;
}).filter(Objects::nonNull).toArray(TransportAddress[]::new);
if (transportAddresses.length == 0) {
throw new UnknownHostException();
}
return transportAddresses;
}
}
- 爪哇8
- Elasticsearch 6.2.4
- 项目使用低级rest客户端从ES获取数据
import static java.net.InetAddress.getByName;
import static java.util.Arrays.stream;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.Objects;
import javax.inject.Inject;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import au.com.api.util.RestClientUtil;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Configuration
public class ElasticConfiguration implements InitializingBean{
@Value(value = "${elasticsearch.hosts}")
private String[] hosts;
@Value(value = "${elasticsearch.httpPort}")
private int httpPort;
@Value(value = "${elasticsearch.tcpPort}")
private int tcpPort;
@Value(value = "${elasticsearch.clusterName}")
private String clusterName;
@Inject
private RestClientUtil client;
@Bean
public RestHighLevelClient restHighClient() {
return new RestHighLevelClient(RestClient.builder(httpHosts()));
}
@Bean
@Deprecated
public RestClient restClient() {
return RestClient.builder(httpHosts()).build();
}
/**
* @return TransportClient
* @throws UnknownHostException
*/
@SuppressWarnings("resource")
@Bean
public TransportClient transportClient() throws UnknownHostException{
Settings settings = Settings.builder()
.put("cluster.name", clusterName).build();
return new PreBuiltTransportClient(settings).addTransportAddresses(transportAddresses());
}
@Override
public void afterPropertiesSet() throws Exception {
log.debug("loading search templates...");
try {
for (Map.Entry<String, String> entry : Constants.SEARCH_TEMPLATE_MAP.entrySet()) {
client.putInlineSearchTemplateToElasticsearch(entry.getKey(), entry.getValue());
}
} catch (Exception e) {
log.error("Exception has occurred in putting search templates into ES.", e);
}
}
private HttpHost[] httpHosts() {
return stream(hosts).map(h -> new HttpHost(h, httpPort, "http")).toArray(HttpHost[]::new);
}
private TransportAddress[] transportAddresses() throws UnknownHostException {
TransportAddress[] transportAddresses = stream(hosts).map(h -> {
try {
return new TransportAddress(getByName(h), tcpPort);
} catch (UnknownHostException e) {
log.error("Exception has occurred in creating ES TransportAddress. host: '{}', tcpPort: '{}'", h, tcpPort, e);
}
return null;
}).filter(Objects::nonNull).toArray(TransportAddress[]::new);
if (transportAddresses.length == 0) {
throw new UnknownHostException();
}
return transportAddresses;
}
}
因此,如果您能看看示例类并在这里与我分享您真正的解决方案,我将不胜感激。我将如何模拟TransportClient
,并从SearchResponse
对象获得正确的响应
注意:
- 我试图使用
中的org.elasticsearch.test:framework:6.2.4
,但遇到了estcase
问题,无法解决。同时,我找不到任何与之相关的文档,或者一般来说,JavaES单元测试李>jar地狱