spring cloud 单元测试 es报错availableP
报错信息
beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'elasticsearchTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchDataAutoConfiguration.class]: Unsatisfied dependency expressed through method 'elasticsearchTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'elasticsearchClient' defined in class path resource [org/springframework/boot/autoconfigure/data/elasticsearch/ElasticsearchAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.elasticsearch.client.transport.TransportClient]: Factory method 'elasticsearchClient' threw exception; nested exception is java.lang.IllegalStateException: availableProcessors is already set to [16], rejecting [16]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject
availableProcessors is already set to [16], rejecting [16]
单元测试类加
static {
System.setProperty("es.set.netty.runtime.available.processors", "false");
}
可以解决
![](https://img.haomeiwen.com/i14033336/ee08c2f64e0631fd.png)
可以抽出一个类,复制启动类中的注解配置,加上上面那段代码,然后test类引用。
@SpringBootTest(classes = TestApplication.class)
![](https://img.haomeiwen.com/i14033336/0085c2013aac69ba.png)
![](https://img.haomeiwen.com/i14033336/b80946366750a273.png)