單元測試的時候,報錯
異常資訊片段
2024-09-03 09:07:12.439 ERROR 5676 --- [ main] c.a.m.c.c.es.ElasticsearchConfig : availableProcessors is already set to [12], rejecting [12]
java.lang.IllegalStateException: availableProcessors is already set to [12], rejecting [12]
at io.netty.util.NettyRuntime$AvailableProcessorsHolder.setAvailableProcessors(NettyRuntime.java:51)
at io.netty.util.NettyRuntime.setAvailableProcessors(NettyRuntime.java:87)
at org.elasticsearch.transport.netty4.Netty4Utils.setAvailableProcessors(Netty4Utils.java:82)
at org.elasticsearch.transport.netty4.Netty4Transport.<init>(Netty4Transport.java:138)
at org.elasticsearch.transport.Netty4Plugin.lambda$getTransports$0(Netty4Plugin.java:93)
at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:174)
at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:265)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:130)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:116)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:106)
解決方案
@Slf4j
@SpringBootTest(classes = Application.class)
@RunWith(SpringRunner.class)
class ControllerTest {
// 新增這一段靜態程式碼塊
static {
System.setProperty("es.set.netty.runtime.available.processors", "false");
}
// ...省略
}