我正在尝试为连接类的配置类创建单元测试。以下是我的连接配置类:
@Configuration
public class JmsMessageGatewayConnectionConfig {
@Bean
public JmsMessageGatewayConnection jmsMessageGatewayConnection (final JmsMessageGatewayProperties jmsConfig) throws JMSException {
return new JmsMessageGatewayConnection(jmsConfig, cachingConnectionFactory(jmsConfig));
}
private CachingConnectionFactory cachingConnectionFactory(final JmsMessageGatewayProperties jmsConfig) {
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
cachingConnectionFactory.setTargetConnectionFactory(jmsConnectionFactory(jmsConfig));
cachingConnectionFactory.resetConnection();
return cachingConnectionFactory;
}
private JmsConnectionFactory jmsConnectionFactory(final JmsMessageGatewayProperties jmsConfig) {
JmsConnectionFactory jmsConnectionFactory =
new JmsConnectionFactory(jmsConfig.getUsername(), jmsConfig.getPassword(), jmsConfig.getRemoteUri());
jmsConnectionFactory.setReceiveLocalOnly(true);
return jmsConnectionFactory;
}
@Bean
@ConfigurationProperties(prefix = "jms")
public JmsMessageGatewayProperties messageGatewayProperties() {
return new JmsMessageGatewayProperties();
}
}
下面是JmsMessageGatewayProperties类:
public class JmsMessageGatewayProperties {
private String remoteUri;
private String username;
private String password;
private boolean messagePersistent;
private Integer forceDetachedRetryLimit = 1;
public String getRemoteUri() {
return remoteUri;
}
public void setRemoteUri(final String remoteUri) {
this.remoteUri = remoteUri;
}
public String getUsername() {
return username;
}
public void setUsername(final String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(final String password) {
this.password = password;
}
public boolean isMessagePersistent() {
return messagePersistent;
}
public void setMessagePersistent(final boolean messagePersistent) {
this.messagePersistent = messagePersistent;
}
public Integer getForceDetachedRetryLimit() {
return forceDetachedRetryLimit;
}
public void setForceDetachedRetryLimit(final Integer forceDetachedRetryLimit) {
this.forceDetachedRetryLimit = forceDetachedRetryLimit;
}
}
下面是我的测试类:
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { JmsMessageGatewayConnectionConfig.class})
@TestPropertySource(locations = "classpath:camel.properties")
public class JmsMessageGatewayConnectionConfigTest {
@Autowired
private JmsMessageGatewayConnection jmsMessageGatewayConnection;
@Test
public void jmsMessageGatewayConnectionConfigTest() {
Assert.assertNotNull(jmsMessageGatewayConnection);
}
}
Invalid URI: cannot be null or empty
的测试失败。我想我通过在调试模式下检查jmsConfig
中的属性来了解它是空的。我确实更新了我的camel.properties,使其具有如下属性:
jms.remoteUri=vm://localhost:61616
jms.username=username
jms.password=password
我不确定我在这里遗漏了什么。为什么JmsMessageGatewayProperties内部的属性为空,即使它有对象?
2条答案
按热度按时间pxy2qtax1#
您是否尝试过添加@DependsOn,因为实现确实要求首先存在配置属性?@DependsOn允许您在创建Bean之前定义Bean的依赖关系。
另外,这取决于你使用的是哪种Spring。你使用的是Spring Boot吗?
或在类别中定义属性指涉。
对Spring进行了一些更改,以实现以下功能:
pdsfdshx2#
我不知道为什么,但是在
@Configuration
类中定义用@ConfigurationProperties
注解的bean会在测试中产生此错误:
导致错误的原因:java.lang.在'com.example.springbootconsumer. JmsMessageGatewayProperties'上找不到组态属性注解。
测试类别:
可能的解决方法是为属性定义Bean,如下所示:
并将其注入到配置类中:
测试类别:
使用Spring引导2.7.3希望这对您有所帮助