如何在AWS上运行的Spring Boot应用程序上本地运行集成测试?

如何在AWS上运行的Spring Boot应用程序上本地运行集成测试?,spring,amazon-web-services,spring-boot,amazon-ec2,spring-cloud,Spring,Amazon Web Services,Spring Boot,Amazon Ec2,Spring Cloud,我正在开发一个Spring引导应用程序,运行在AWS上。我已经安装了Spring Cloud AWS starter,但是当我尝试在笔记本电脑上本地运行集成测试时,我遇到了这个错误 创建名为“org.springframework.cloud.aws.context.support.io.ResourceLoaderBeanPostProcessor#0”的bean时出错:设置构造函数参数时无法解析对bean“amazonS3”的引用;嵌套异常为org.springframework.beans

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

创建名为“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的情况下运行我的应用程序?仅用于本地集成测试。

考虑添加使用外部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,它就不会被创建


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

考虑添加使用外部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,它就不会被创建


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

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