gpt4 book ai didi

java - 如何为 Spring 测试配置嵌套依赖?

转载 作者:行者123 更新时间:2023-11-30 06:55:57 41 4
gpt4 key购买 nike

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

具体错误:由以下原因引起:java.net.URISyntaxException:索引 8 处的权限中存在非法字符:https://${sqs.endpoint}

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

@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;
}
}

这里是注入(inject)这个@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”网址。

我认为这是将属性注入(inject)测试类的更简洁的方法之一。

关于java - 如何为 Spring 测试配置嵌套依赖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41814498/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com