【问题标题】:How to configure nested dependency for spring tests?如何为 Spring 测试配置嵌套依赖项?
【发布时间】:2017-01-23 19:54:49
【问题描述】:

我遇到的错误与在注入到 Test 类之前解析属性有关。注入属性时,我最终得到${property.name}。但是,考虑到存在嵌套依赖关系,Test 类的配置似乎非常错误。

具体错误:Caused by: java.net.URISyntaxException: Illegal character in authority at index 8: https://${sqs.endpoint}

我有一个配置类来为 @Bean 加载特定的道具:

@Configuration
public class AWSConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(AWSConfig.class);
    private @Value("${sqs.endpoint}") String endpoint;

    @Bean(name = "awsClient")
    @Primary
    public AmazonSQSAsyncClient amazonSQSClient() {
        AmazonSQSAsyncClient awsSQSAsyncClient
                = new AmazonSQSAsyncClient();

        awsSQSAsyncClient.setEndpoint(endpoint);
        return awsSQSAsyncClient;
    }
}

这是@Bean 的注入位置:

@Component
public class SqsQueueSender {

    private static final Logger LOGGER = LoggerFactory.getLogger(SqsQueueSender.class);
    private final QueueMessagingTemplate queueMessagingTemplate;

    @Autowired
    @Qualifier("awsClient")
    AmazonSQSAsyncClient amazonSQSAsyncClient;

    public SqsQueueSender(AmazonSQSAsync amazonSQSAsyncClient) {
        this.queueMessagingTemplate = new QueueMessagingTemplate(amazonSQSAsyncClient);
    }

    //take advantage of convertAndSend to send POJOs in appropriate format
    public void send(String queueName, String message) {
        this.queueMessagingTemplate.convertAndSend(queueName, MessageBuilder.withPayload(message).build());
    }
}

这一切似乎都有效,至少应用程序启动并从任一位置打印日志。不过,我无法针对此代码运行单元测试。我不知道如何正确设置配置。这是测试类的最新迭代:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader=AnnotationConfigContextLoader.class)
public class SqsQueueSenderTest {

    @Configuration
    static class ContextConfiguration {

        private @Value("${sqs.endpoint}") String endpoint;

        @Bean(name = "awsClient")
        @Primary
        public AmazonSQSAsyncClient amazonSQSClient() {
            AmazonSQSAsyncClient awsSQSAsyncClient
                    = new AmazonSQSAsyncClient();

            awsSQSAsyncClient.setEndpoint(endpoint);
            return awsSQSAsyncClient;
        }

        @Bean
        public SqsQueueSender sqsQueueSender() {
            SqsQueueSender sqsQueueSender = new SqsQueueSender(amazonSQSClient());

            // set up the client
            return sqsQueueSender;
        }
    }

    @Autowired
    SqsQueueSender sqsQueueSender;// = new SqsQueueSender(new AmazonSQSAsyncClient());


    private static final Logger LOGGER = LoggerFactory.getLogger(SqsQueueSenderTest.class);

    // attributes for in-memory sqs server
    AmazonSQSClient client;
    SQSRestServer server;
    SQSRestServerBuilder sqsRestServerBuilder;


    @Before
    public void startup() {
        LOGGER.info("Building in-memory SQS server");
        this.server = sqsRestServerBuilder.withPort(9324).withInterface("localhost").start();
        this.client = new AmazonSQSClient(new BasicAWSCredentials("x", "x"));
        client.setEndpoint("http://localhost:9324");
        client.createQueue("test");
        LOGGER.info("Finished building in-memory SQS server");
    }

    @After
    public void shutdown() {
        LOGGER.info("Stopping in-memory SQS server");
        server.stopAndWait();
        LOGGER.info("Finished stopping in-memory SQS server");
    }

    @Test
    public void testSending() {
        LOGGER.info("~~~~~~~~~~~~~");
        sqsQueueSender.send("test", "new message");
        LOGGER.info("The current queues are" + client.listQueues().toString());
        LOGGER.info("~~~~~~~~~~~~~");
    }
}

【问题讨论】:

    标签: java spring spring-boot dependency-injection configuration


    【解决方案1】:

    Joe,首先将你的连接属性放在一个资源中进行测试:

    src/test/resouces/test.properties
    

    然后将其添加到 Test 类定义中:

    @PropertySource(
              value={"classpath:test.properties"},
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(loader=AnnotationConfigContextLoader.class)
    public class SqsQueueSenderTest {
    

    最后在你的配置类中添加这个bean:

    @Configuration static class ContextConfiguration {
    
         @Bean
         public static PropertySourcesPlaceholderConfigurer properties() throws Exception {
                return new PropertySourcesPlaceholderConfigurer();
         }
    }
    

    不要忘记在属性文件中放置“sqs.endpoint”网址。

    在我看来,这是将属性注入测试类的更简洁的方法之一。

    【讨论】:

    • 谢谢,但我实际上把PropertySource放在了内部@Configuration类上,实际上还得在test/resources目录下新建一个props文件。
    • 这就是我对测试文件的意思:)。是的,您也可以将注释放在 bean 本身上。不管怎样,很高兴它对你有用
    猜你喜欢
    • 2020-03-23
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 2016-01-04
    相关资源
    最近更新 更多