alibaba / alibaba/testable-mock
no unique public constructor for [org.elasticsearch.transport.Netty4Plugin]
- Dominant language
- Java
- Stars
- 1.8k
- Forks
- 306
- PR merge metrics
- No merged PRs in 30d
Description
项目框架:springboot 1.5.9
测试用例
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:spring-config.xml")
public class XxMapperTest {
@Resource
private XxMapper xxMapper;
@Test
public void test_get_xx() {
Entity entity=new Entity();
entity.setA(System.currentTimeMillis());
entity.setB(1);
xxMapper.insert(entity);
Entity queryEntity= xxMapper.getXx(entity.getA(), entity.getB());
logger.error("queryEntity---"+ JSON.toJSONString(queryEntity));
Assert.assertTrue(entity.getA().equals(queryEntity.getA()));
Assert.assertTrue(entity.getB().equals(queryEntity.getB()));
}
}
说明:项目中有引入es框架
org.springframework.data
spring-data-elasticsearch
3.1.19.RELEASE
报错:
Caused by: java.lang.IllegalStateException: no unique public constructor for [org.elasticsearch.transport.Netty4Plugin]
at org.elasticsearch.plugins.PluginsService.loadPlugin(PluginsService.java:543)
at org.elasticsearch.plugins.PluginsService.(PluginsService.java:104)
at org.elasticsearch.client.transport.TransportClient.newPluginService(TransportClient.java:105)
at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:130)
at org.elasticsearch.client.transport.TransportClient.(TransportClient.java:273)
at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:128)
at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:114)
at org.elasticsearch.transport.client.PreBuiltTransportClient.(PreBuiltTransportClient.java:104)
at com.jd.paimai.user.rights.search.service.es.esclient.SpringDataEsTransportClient.getObject(SpringDataEsTransportClient.java:86)
at com.jd.paimai.user.rights.search.service.es.esclient.SpringDataEsTransportClient.getObject(SpringDataEsTransportClient.java:27)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)
... 62 more
后经断点运行发现,走到了testable下的com.alibaba.testable.agent.transformer.TestableClassTransformer#transform
然后这个方法mock了一个org.elasticsearch.transport.Netty4Plugin,同时构造方法也多了一个
public org.elasticsearch.transport.Netty4Plugin(java.lang.Void),加上本身自己的构造器,构造器变成了2个
org.elasticsearch.plugins.PluginsService#loadPlugin方法会对构造器内容做校验,导致异常:
private Plugin loadPlugin(Class pluginClass, Settings settings, Path configPath) {
Constructor[] constructors = pluginClass.getConstructors();
if (constructors.length == 0) {
throw new IllegalStateException("no public constructor for [" + pluginClass.getName() + "]");
} else if (constructors.length > 1) {
throw new IllegalStateException("no unique public constructor for [" + pluginClass.getName() + "]");
} else {
Constructor constructor = constructors[0];
if (constructor.getParameterCount() > 2) {
throw new IllegalStateException(this.signatureMessage(pluginClass));
} else {
Class[] parameterTypes = constructor.getParameterTypes();
try {
if (constructor.getParameterCount() == 2 && parameterTypes[0] == Settings.class && parameterTypes[1] == Path.class) {
return (Plugin)constructor.newInstance(settings, configPath);
} else if (constructor.getParameterCount() == 1 && parameterTypes[0] == Settings.class) {
return (Plugin)constructor.newInstance(settings);
} else if (constructor.getParameterCount() == 0) {
return (Plugin)constructor.newInstance();
} else {
throw new IllegalStateException(this.signatureMessage(pluginClass));
}
} catch (ReflectiveOperationException var8) {
throw new IllegalStateException("failed to load plugin class [" + pluginClass.getName() + "]", var8);
}
}
}
}
Contributor guide
No contributing guide indexed for this repository
Research direction
Reproduce the SpringJUnit4ClassRunner test with the spring-data-elasticsearch dependency and inspect TestableClassTransformer#transform when it processes org.elasticsearch.transport.Netty4Plugin. Compare the plugin constructors before and after transformation, then verify the Elasticsearch TransportClient initialization no longer raises the unique-constructor error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- elasticsearch, java, spring-boot
- Domain
- testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100