【问题标题】:Unit testing a AWS Spring Boot Configuration file - possible?对 AWS Spring Boot 配置文件进行单元测试 - 可能吗?
【发布时间】:2017-02-18 05:43:05
【问题描述】:

我们目前正在使用 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 框架实际上需要先运行才能注入这些配置值?我们需要在这里进行集成测试吗?

使用可行的解决方案进行编辑...

使用公认的答案,这是我的工作测试!我能够删除我对 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);
    }
}

【问题讨论】:

    标签: java spring amazon-web-services spring-boot


    【解决方案1】:

    您是否尝试过向您的类注入模拟(或自动装配),然后使用 ReflectionTestUtils 设置该字段?这是 Spring 提供的一个很好的测试 utils 类,它允许您在不修改代码的情况下执行您想要的操作。 我的意思是这样的:

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

    【讨论】:

    • 我对尝试利用框架的自动配置非常感兴趣——我什至没有想到使用反射注入。效果很好 - 谢谢!我已将工作测试类添加到我的问题中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    • 2017-12-25
    • 2017-10-21
    相关资源
    最近更新 更多