How can I run my integration tests locally on a Spring Boot application running on AWS?

Viewed 847

I'm developing a Spring Boot application, running on AWS. I've installed Spring Cloud AWS starter, but when I try to run integration tests locally, on my laptop, I'm having this error.

Error creating bean with name 'org.springframework.cloud.aws.context.support.io.ResourceLoaderBeanPostProcessor#0': Cannot resolve reference to bean 'amazonS3' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'amazonS3': Invocation of init method failed; nested exception is java.lang.IllegalStateException: There is not EC2 meta data available, because the application is not running in the EC2 environment. Region detection is only possible if the application is running on a EC2 instance

Is there a way of running my application without AWS? Only for local integration tests purpose.

2 Answers

Consider adding fake implementations of your classes, that use external API. You could use them only in test profile. For example:

@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

  }
}

And you can turn off aws in default profile.

So it wouldn't go to AWS.

Just don't inject AmazonS3 to beans in default profile and it wouldn't be created.

Hope I understood your question properly.

You could create a Configuration class for a test profile which excludes the Spring auto-configuration classes related to AWS. For example:

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 {

    
}
Related