Java Junit测试和spring@配置中的自动连线失败
我有两个Java Junit测试和spring@配置中的自动连线失败,java,spring,junit,autowired,Java,Spring,Junit,Autowired,我有两个@Configuration类。我需要一个从一个配置类到另一个配置类的bean。我已将配置1自动连接到2。一切正常。当执行单元测试时,我得到以下异常 setUpContext(com.trafigura.titan.framework.services.messaging.loader.SpringLoadTest) org.springframework.beans.factory.BeanCreationException: Error creating bean with name
@Configuration
类。我需要一个从一个配置类到另一个配置类的bean。我已将配置1自动连接到2。一切正常。当执行单元测试时,我得到以下异常
setUpContext(com.trafigura.titan.framework.services.messaging.loader.SpringLoadTest)
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.xxx.MessagingServicesConfig': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.xxx.EMSJMSConfig com.xxx.MessagingServicesConfig.emsJmsConfig;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type[com.xxx.EMSJMSConfig] 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)}
还有什么我需要做的额外工作,使这个工作
下面是测试的设置
@Configuration
@Import({MessagingServicesConfig.class,...,EMSJMSConfig.class
})
public class MessagingConfig {}
@Profile("EMS-MESSAGING")
@Configuration
public class EMSJMSConfig {
@Bean
public javax.jms.ConnectionFactory jmsSubscriberConnectionFactory() throws JMSException {
SingleConnectionFactory singleConnectionFactory = new SingleConnectionFactory(tibjmsConnectionFactory());
return singleConnectionFactory;
}
}
@Configuration
public class MessagingServicesConfig {
@Autowired
private EMSJMSConfig emsJmsConfig;
@Bean(destroyMethod = "shutdown")
public MessagingService messagingService() throws JMSException {
...
ConnectionFactory cf=emsJmsConfig.jmsSubscriberConnectionFactory(); // Getting NPE at this line.
}
}
最后是测试类
public class MessagingServicesConfigTest {
private MessagingServicesConfig config;
private EMSJMSConfig emsJmsConfig;
@BeforeMethod
public void setUp() throws Exception {
config = new MessagingServicesConfig();
... //what needs to be done here to have the EMSJMSConfig
}
@Test
public void testBuildsCorrectService() throws JMSException {
MessagingService service = config.messagingService();
...
}
}
通过调用
new
您自己正在创建对象,Spring对此一无所知
此外,您应该有一个测试配置,它将知道您的bean
使用适当的Runner
加载SpringContext
@ContextConfiguration(classes = TestConfig.class)
@RunWith(SpringRunner.class)
class Tests {
@Autowired // if needed
private MessagingServicesConfig config;
}
在TestConfig
中,您可以创建bean
或从应用程序导入配置:
@Configuration
@Import({MessagingServicesConfig.class})
public class TestConfig {}
@Configuration
@Import({EMSJMSConfig.class})
public class MessagingServicesConfig {}
或者,您可以直接参考配置类:
@ContextConfiguration(classes = {MessagingServicesConfig.class, EMSJMSConfig.class})
这不会有帮助,也不会加载配置。使用适当的
@ContextConfiguration
注释指定要加载的内容。然后请告诉我如何加载它?如果看不到您现在正在做什么,很难判断。但是您应该使用SpringRunner
来运行测试,并使用@ContextConfiguration
来指定要加载的类。如果没有@ContextConfiguration
,这将不会有多大作用(我怀疑会发出警告和错误)。@M.Deinum,我的坏,发布不完整。