【问题标题】:How can I run my integration tests locally on a Spring Boot application running on AWS?如何在 AWS 上运行的 Spring Boot 应用程序上本地运行集成测试?
【发布时间】:2018-03-15 19:54:44
【问题描述】:

我正在开发一个在 AWS 上运行的 Spring Boot 应用程序。我已经安装了 Spring Cloud AWS 启动器,但是当我尝试在我的笔记本电脑上本地运行集成测试时,我遇到了这个错误。

使用名称“org.springframework.cloud.aws.context.support.io.ResourceLoaderBeanPostProcessor#0”创建 bean 时出错:在设置构造函数参数时无法解析对 bean“amazonS3”的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“amazonS3”的 bean 时出错:调用 init 方法失败;嵌套异常是 java.lang.IllegalStateException: 没有可用的 EC2 元数据,因为应用程序没有在 EC2 环境中运行。只有当应用程序在 EC2 实例上运行时,才能进行区域检测

有没有办法在没有 AWS 的情况下运行我的应用程序?仅用于本地集成测试。

【问题讨论】:

  • 如果您在 AWS 环境之外运行应用程序。为避免该错误,请在 application.properties 文件中指定区域手册,如下所示cloud.aws.region.static=eu-west-1
  • 这解决了我的问题。

标签: spring amazon-web-services spring-boot amazon-ec2 spring-cloud


【解决方案1】:

考虑添加使用外部 API 的类的虚假实现。 您只能在测试配置文件中使用它们。 例如:

@Component
@Profile("default")
public class FakePhotosUploader implements Photos {

  @Override
  public String uploadPhoto(byte[] bytes, String name, Integer receiptId) {
    return UUID.randomUUID().toString();
    //you can write here some implementation, suitable for your tests

  }
}

您可以在默认配置文件中关闭 aws。

所以它不会转到 AWS。

只是不要将 AmazonS3 注入默认配置文件中的 bean,它就不会被创建。

希望我能正确理解您的问题。

【讨论】:

    【解决方案2】:

    您可以为测试配置文件创建一个配置类,其中不包括与 AWS 相关的 Spring 自动配置类。例如:

    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Profile;
    
    import io.awspring.cloud.autoconfigure.context.ContextCredentialsAutoConfiguration;
    import io.awspring.cloud.autoconfigure.context.ContextResourceLoaderAutoConfiguration;
    
    @Configuration
    @EnableAutoConfiguration(exclude= {ContextCredentialsAutoConfiguration.class,ContextResourceLoaderAutoConfiguration.class})
    @Profile("test")
    public class AwsTestConfiguration {
    
        
    }
    

    【讨论】:

      猜你喜欢
      • 2020-04-25
      • 2017-04-19
      • 2023-03-11
      • 2020-02-10
      • 2015-08-12
      • 2019-08-09
      • 2020-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多