spring 如何为具有属性的配置类创建单元测试?

ymdaylpp  于 2022-11-21  发布在  Spring
关注(0)|答案(2)|浏览(132)

我正在尝试为连接类的配置类创建单元测试。以下是我的连接配置类:

@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内部的属性为空,即使它有对象?

pxy2qtax

pxy2qtax1#

您是否尝试过添加@DependsOn,因为实现确实要求首先存在配置属性?@DependsOn允许您在创建Bean之前定义Bean的依赖关系。
另外,这取决于你使用的是哪种Spring。你使用的是Spring Boot吗?
或在类别中定义属性指涉。

@PropertySource("classpath:camel.properties")
public class JmsMessageGatewayProperties  {}

对Spring进行了一些更改,以实现以下功能:

@ConfigurationPropertiesScan 
@ConfigurationProperties(prefix = "jms")
public JmsMessageGatewayProperties messageGatewayProperties() {
    return new JmsMessageGatewayProperties();
}
pdsfdshx

pdsfdshx2#

我不知道为什么,但是在@Configuration类中定义用@ConfigurationProperties注解的bean

@Bean
@ConfigurationProperties(prefix = "jms")
public JmsMessageGatewayProperties messageGatewayProperties() {
    return new JmsMessageGatewayProperties();
}

会在测试中产生此错误:
导致错误的原因:java.lang.在'com.example.springbootconsumer. JmsMessageGatewayProperties'上找不到组态属性注解。
测试类别:

import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@ExtendWith(SpringExtension.class)
@EnableConfigurationProperties(value = JmsMessageGatewayProperties.class)
@ContextConfiguration(classes = JmsMessageGatewayConnectionConfig.class)
@TestPropertySource(locations = "classpath:camel.properties")
public class JmsMessageGatewayConnectionConfigTest {

    @Autowired
    private JmsMessageGatewayProperties jmsMessageGatewayProperties;

    @Test
    public void jmsMessageGatewayConnectionConfigTest() {
        assertNotNull(jmsMessageGatewayProperties);
    }
}

可能的解决方法是为属性定义Bean,如下所示:

@Configuration
@ConfigurationProperties(prefix = "jms")
public class JmsMessageGatewayProperties {
    
    private String remoteUri;
    private String username;
    private String password;
    private boolean messagePersistent;
    private Integer forceDetachedRetryLimit = 1;
    //getters setters
}

并将其注入到配置类中:

@Configuration
public class JmsMessageGatewayConnectionConfig {
    @Autowired
    private JmsMessageGatewayProperties jmsConfig;
    @Bean
    public JmsMessageGatewayConnection jmsMessageGatewayConnection () throws JMSException {
        return new JmsMessageGatewayConnection(jmsConfig, cachingConnectionFactory(jmsConfig));
    }

    private CachingConnectionFactory cachingConnectionFactory() {
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
        cachingConnectionFactory.setTargetConnectionFactory(jmsConnectionFactory(jmsConfig));
        cachingConnectionFactory.resetConnection();
        return cachingConnectionFactory;
    }

    private JmsConnectionFactory jmsConnectionFactory() {
        JmsConnectionFactory jmsConnectionFactory =
                new JmsConnectionFactory(jmsConfig.getUsername(), jmsConfig.getPassword(), jmsConfig.getRemoteUri());
        jmsConnectionFactory.setReceiveLocalOnly(true);
        return jmsConnectionFactory;
    }
}

测试类别:

@ExtendWith(SpringExtension.class)
@EnableConfigurationProperties(value = JmsMessageGatewayProperties.class)
@ContextConfiguration(classes = JmsMessageGatewayConnectionConfig.class)
@TestPropertySource(locations = "classpath:camel.properties")
public class JmsMessageGatewayConnectionConfigTest {

    @Autowired
    private JmsMessageGatewayConnection jmsMessageGatewayConnection;

    @Test
    public void jmsMessageGatewayConnectionConfigTest() {
        Assert.assertNotNull(jmsMessageGatewayConnection);
    }
}

使用Spring引导2.7.3希望这对您有所帮助

相关问题