I have a SpringBoot application which I am trying to test. I’m struggling with this test
@SpringBootTest
@ActiveProfiles("test")
public class LicenceApplicationTests
{
@Test
@DisplayName("Context initialised correctly.")
public void contextLoads(ApplicationContext context)
{
assertThat(context).isNotNull();
}
}
The error I’m getting is:
java.lang.RuntimeException: Error retrieving secret: Invalid name. Must be a valid name containing alphanumeric characters, or any of the following: -/_+=.@! (Service: SecretsManager, Status Code: 400, Request ID: 7d7094ab-9216-4c97-9414-958c4bb95717)
and can be traced back to this point in my code, which is a method in a @Service class:
public static String getSecretValue(SecretsManagerClient secretsClient, String secretName)
{
try
{
GetSecretValueRequest valueRequest = GetSecretValueRequest.builder()
.secretId(secretName)
.build();
GetSecretValueResponse valueResponse = secretsClient.getSecretValue(valueRequest);
return valueResponse.secretString();
}
catch (SecretsManagerException e)
{
throw new RuntimeException("Error retrieving secret: " + e.getMessage(), e);
}
}
Usually, I would provide the secret name as an environment variable. However, I feel it would be bad to do this here, because that means the test passing would rely on the existence of a given AWS secret.
As such, I want to disable secrets manager (and also S3!) for my test profile, s.t. the test doesn’t fail simply because it can’t find any real secrets or S3 buckets to access.
Here is the relevant part of my application.yml:
aws:
s3:
bucket:
name: ${S3_BUCKET_NAME:}
key: ${S3_BUCKET_KEY:}
secret:
passphrase: ${AWS_SECRET_NAME_PASSPHRASE:}
I have tried putting this in my application-test.yml (to no avail). I also tried adding a bootstrap.yml file with a similar looking bit of code and it didn’t work.
spring:
cloud:
aws:
region:
static: eu-west-2
s3:
enabled: false
secretsmanager:
enabled: false
martin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.