如何为spring测试配置嵌套依赖项?

时间:2017-01-23 19:54:49

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

我得到的错误与在注入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("~~~~~~~~~~~~~");
    }
}

1 个答案:

答案 0 :(得分:1)

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

src/test/resouces/test.properties

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

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

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

@Configuration static class ContextConfiguration {

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

别忘了放置' sqs.endpoint'您的属性文件中的网址。

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