gpt4 book ai didi

java - 对 AWS Spring Boot 配置文件进行单元测试 - 可能吗?

转载 作者:行者123 更新时间:2023-12-01 14:28:59 30 4
gpt4 key购买 nike

我们目前正在使用 Spring Boot 连接到 Amazon SQS 的模拟本地实例。应用程序本身在运行时可以正常工作,但如果可能并且有意义的话,我们想尝试测试 SQS Config 类。

这里是配置类。当 Spring 应用程序本身运行时,所有属性都从典型的 application.properties 文件中提取。

import com.amazonaws.services.sqs.AmazonSQSAsync;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.aws.core.env.ResourceIdResolver;
import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AWSSQSConfig {
@Value("${aws.sqs.endpoint}")
private String AWSSqsEndpoint;

// Producer QueueMessageTemplate
@Bean
public QueueMessagingTemplate queueMessagingTemplate(AmazonSQSAsync amazonSqs, ResourceIdResolver resourceIdResolver) {
if (!AWSSqsEndpoint.isEmpty())
amazonSqs.setEndpoint(AWSSqsEndpoint);

return new QueueMessagingTemplate(amazonSqs, resourceIdResolver);
}
}

这是测试类。我们试图通过 TestPropertySource 传递配置,但它们实际上似乎并没有到达 AWSSQSConfig 类。类实例中的 AWSSqsEndpoint 始终为 NULL。

import com.amazonaws.services.sqs.AmazonSQSAsync;
import com.lonewolf.formsbuilder.config.AWSSQSConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.aws.core.env.ResourceIdResolver;
import org.springframework.cloud.aws.messaging.core.QueueMessagingTemplate;
import org.springframework.test.context.TestPropertySource;

import static org.junit.Assert.assertNotNull;

@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
@TestPropertySource(properties = {
"cloud.aws.region.static=us-east-1",
"cloud.aws.credentials.accessKey=zzzzz",
"cloud.aws.credentials.secretKey=zzzzzz",

"aws.sqs.endpoint = http://localhost:9324",
"aws.sqs.requestQueue = CreateSchemaRequest",
"aws.sqs.responseQueue = CreateSchemaResponse"
})
public class AWSSQSConfigTests {
@Mock
private AmazonSQSAsync amazonSqs;

@Mock
private ResourceIdResolver resourceIdResolver;

@Test
public void contextLoads() {
AWSSQSConfig config = new AWSSQSConfig();

QueueMessagingTemplate queueMessagingTemplate = config.queueMessagingTemplate(amazonSqs, resourceIdResolver);

assertNotNull("The response body must not be null", queueMessagingTemplate);
}
}

这是先有鸡还是先有蛋的情况,spring 框架实际上需要先运行才能注入(inject)那些配置值?我们需要在这里进行集成测试吗?

编辑工作解决方案...

使用已接受的答案,这是我的工作测试!我能够移除我对 Spring 框架的依赖。

@RunWith(MockitoJUnitRunner.class)
public class AWSSQSConfigTests {
@Mock
private AmazonSQSAsync amazonSqs;

@Mock
private ResourceIdResolver resourceIdResolver;

@InjectMocks
private AWSSQSConfig config;

@Before
public void setup() {
ReflectionTestUtils.setField(config, "AWSSqsEndpoint", "http://fake");
}

@Test
public void contextLoads() {
QueueMessagingTemplate queueMessagingTemplate = config.queueMessagingTemplate(amazonSqs, resourceIdResolver);

assertNotNull("The response body must not be null", queueMessagingTemplate);
}
}

最佳答案

您是否尝试过将 mock 注入(inject)您的类(或 Autowiring 它),然后使用 ReflectionTestUtils 设置该字段?这是 Spring 提供的一个很好的测试实用程序类,它允许您在不修改代码的情况下执行您想要的操作。我的意思是这样的:

    @InjectMocks
private AWSSQSConfig awssqsConfig;

@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ReflectionTestUtils.setField(awssqsConfig, "AWSSqsEndpoint", "putYourEndpointHere");
}

关于java - 对 AWS Spring Boot 配置文件进行单元测试 - 可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42260849/

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