大家好我想测试我的弹性搜索连接Bean(EsConnection)
我有4个属性host,port和clusterName,它们是从属性文件
加载的和TransportClient创建与@Autowired
注释的elasticsearch的连接这是我的连接类
@Component
@PropertySource("classpath:elasticsearch.properties")
public class EsConnection {
@Autowired
TransportClient client;
@Value("${elasticsearch.dev.host}")
private String host;
@Value("${elasticsearch.dev.port}")
private int port;
@Value("${elasticsearch.dev.clusterName}")
private String clusterName;
@PostConstruct
public void connect() {
InetSocketTransportAddress node;
try {
node = new InetSocketTransportAddress(InetAddress.getByName(this.host), this.port);
// settings elasticsearch
Settings settings = Settings.builder().put("cluster.name", this.clusterName)
.put("client.transport.sniff", true).build();
this.client = new PreBuiltTransportClient(settings);
client.addTransportAddress(node);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public EsConnectionManagerBiz(String host, int port, String clusterName) {
super();
this.host = host;
this.port = port;
this.clusterName = clusterName;
}
public EsConnectionManagerBiz() {
super();
// TODO Auto-generated constructor stub
}
public TransportClient getClient() {
return client;
}
public void setClient(TransportClient client) {
this.client = client;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getClusterName() {
return clusterName;
}
public void setClusterName(String clusterName) {
this.clusterName = clusterName;
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}
}
这是我的班级考试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:*/spring/app-config.xml"})
public class Test {
@Autowired
EsConnection es;
@Test
public void testAnnuaire() {
System.out.println("show message");
}
}
app-config.xml我有一个包含EsConnection类包名称的打包扫描
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
09:52:27,509 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
09:52:27,509 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback-test.xml] at [file:/D:/Intelcom/IAM%202018/kd/Workspaces/BR_FC_599_Migration_Daleel_dans_kd/kd-zz/target/test-classes/logback-test.xml]
09:52:27,585 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
09:52:27,587 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeFilter scanning period to 30 seconds
09:52:27,587 |-INFO in ch.qos.logback.classic.turbo.ReconfigureOnChangeFilter@2a602a60 - Will scan for changes in [] every 30 seconds.
09:52:27,587 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter
09:52:27,588 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
09:52:27,593 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [FILE]
09:52:27,671 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use zip compression
09:52:27,672 |-INFO in c.q.l.core.rolling.TimeBasedRollingPolicy - Will use the pattern target/logs/kd-zz-test.%d{yyyy-MM-dd}.log for the active file
09:52:27,678 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - The date pattern is 'yyyy-MM-dd' from file name pattern 'target/logs/kd-zz-test.%d{yyyy-MM-dd}.log.zip'.
09:52:27,678 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Roll-over at midnight.
09:52:27,682 |-INFO in c.q.l.core.rolling.DefaultTimeBasedFileNamingAndTriggeringPolicy - Setting initial period to Thu Dec 21 09:45:39 WET 2017
09:52:27,706 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - This appender no longer admits a layout as a sub-component, set an encoder instead.
09:52:27,706 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
09:52:27,706 |-WARN in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
09:52:27,706 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - Active log file name: target/logs/kd-zz-test.log
09:52:27,706 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - File property is set to [target/logs/kd-zz-test.log]
09:52:27,706 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
09:52:27,708 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [CONSOLE]
09:52:27,720 |-WARN in ch.qos.logback.core.ConsoleAppender[CONSOLE] - This appender no longer admits a layout as a sub-component, set an encoder instead.
09:52:27,720 |-WARN in ch.qos.logback.core.ConsoleAppender[CONSOLE] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
09:52:27,720 |-WARN in ch.qos.logback.core.ConsoleAppender[CONSOLE] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
09:52:27,720 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [fr.projet] to false
09:52:27,720 |-INFO in ch.qos.logback.classic.joran.action.LevelAction - fr.projet level set to DEBUG
09:52:27,720 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE] to Logger[fr.projet]
09:52:27,723 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [CONSOLE] to Logger[fr.projet]
09:52:27,723 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [com.googlecode] to false
09:52:27,723 |-INFO in ch.qos.logback.classic.joran.action.LevelAction - com.googlecode level set to DEBUG
09:52:27,723 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE] to Logger[com.googlecode]
09:52:27,723 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [CONSOLE] to Logger[com.googlecode]
09:52:27,723 |-INFO in ch.qos.logback.classic.joran.action.LevelAction - ROOT level set to ERROR
09:52:27,723 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE] to Logger[ROOT]
09:52:27,723 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [CONSOLE] to Logger[ROOT]
09:52:28.218 [main] ERROR o.s.t.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@6f5a6f5a] to prepare test instance [fr.projet.iam.kd.test.zz.Test@47604760]
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fr.projet.iam.kd.test.zz.Test': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: fr.projet.iam.kd.core.zz.EsConnection fr.projet.iam.kd.test.zz.Test.an; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [fr.projet.iam.kd.core.zz.EsConnection] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:374) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:127) ~[spring-test-2.5.2.jar:2.5.2]
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:85) ~[spring-test-2.5.2.jar:2.5.2]
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:277) ~[spring-test-2.5.2.jar:2.5.2]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:95) [spring-test-2.5.2.jar:2.5.2]
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:139) [spring-test-2.5.2.jar:2.5.2]
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51) [junit-4.4.jar:na]
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44) [junit-4.4.jar:na]
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27) [junit-4.4.jar:na]
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37) [junit-4.4.jar:na]
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42) [junit-4.4.jar:na]
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) [.cp/:na]
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) [.cp/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: fr.projet.iam.kd.core.zz.EsConnection fr.projet.iam.kd.test.zz.Test; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [fr.projet.iam.kd.core.zz.EsConnection] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
... 18 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [fr.projet.iam.kd.core.zz.EsConnection] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478) ~[spring-beans-3.1.0.RELEASE.jar:3.1.0.RELEASE]
... 20 common frames omitted