Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为spring测试配置嵌套依赖关系?_Java_Spring_Spring Boot_Dependency Injection_Configuration - Fatal编程技术网

Java 如何为spring测试配置嵌套依赖关系?

Java 如何为spring测试配置嵌套依赖关系?,java,spring,spring-boot,dependency-injection,configuration,Java,Spring,Spring Boot,Dependency Injection,Configuration,我遇到的错误与在注入测试类之前解析属性有关。注入属性时,我以${property.name}结束。然而,考虑到存在嵌套依赖关系,测试类的配置似乎非常错误 特定错误:由以下原因引起:java.net.URISyntaxException:索引8处的权限中的非法字符:https://${sqs.endpoint} 我有一个配置类来为@Bean加载特定的道具: @Configuration public class AWSConfig { private static final Logge

我遇到的错误与在注入测试类之前解析属性有关。注入属性时,我以
${property.name}
结束。然而,考虑到存在嵌套依赖关系,测试类的配置似乎非常错误

特定错误:
由以下原因引起:java.net.URISyntaxException:索引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("~~~~~~~~~~~~~");
    }
}

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

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”url


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

谢谢,但我实际上将
PropertySource
放在内部
@Configuration
类上,并且实际上必须在
test/resources
目录中创建一个新的props文件。这就是我关于测试文件的意思:)。是的,您也可以将注释放在bean本身上。不管怎样,我很高兴它对你有用