Issue
I have a class for my AWS Lambda handler, and it relies on environment variables for things like what region, bucket name, etc. For example:
public class SomethingCoolLambda {
@NonNull private final String region = System.getenv("AWS_REGION");
@NonNull private final String outputBucket = System.getenv("OUTPUT_BUCKET");
public void eventHandler(@NonNull final ScheduledEvent event, @NonNull final Context context) {
// Do cool stuff here, for example:
final CoolQuery coolQuery =
CoolQuery.builder()
.targetBucket(outputBucket)
.sqsClient(SqsClient.builder().region(Region.of(region)).build())
.build();
}
}
This works great. The problem is I'm writing an integration test and the test needs to use a different bucket instead of the live bucket.
Is there a way to instantiate or trigger SomethingCoolLambda()
in a way where I can pass in an alternative bucket name instead of relying on the provided environment variable's value?
I want to avoid modifying the SomethingCoolLambda
class, if possible.
Solution
As Mark pointed out in his comment, my question isn't specific to AWS or Lambda.
If you want to set the value of environment variable specifically for testing, this post explains how to do it: How to test code dependent on environment variables using JUnit?
Answered By - S.S.
Answer Checked By - Timothy Miller (JavaFixing Admin)