如何配置嵌套依赖春季测试?(How to configure nested dependency f

2019-09-29 03:55发布

我得到的错误与注射到实验课前解析性能做。 我结束了${property.name}当地产注入。 然而,测试类的配置显得非常错误的考虑有嵌套的相关性。

具体错误: 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("~~~~~~~~~~~~~");
    }
}

Answer 1:

乔,首先把你的连接属性中进行测试的资源:

src/test/resouces/test.properties

然后将其添加到测试类定义:

@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”网址。

这在我看来是注入你的属性到测试类的更清洁的方式之一。



文章来源: How to configure nested dependency for spring tests?