0

My specific problem is that a bean being instantiated is looking in the wrong location for a file. In my app's config, that value is never defined and is a default for the bean.

Here's my app config:

@Configuration
public class AWSConfig {

    private static final Logger LOGGER = LoggerFactory.getLogger(AWSConfig.class);

    // base sqs endpoint url is injected from application.properties
    @Bean(name="awsClient")
    @Primary
    public AmazonSQSAsyncClient amazonSQSClient() {
        LOGGER.info("Instantiating AmazonSQSAsyncClient Bean");
        AmazonSQSAsyncClient awsSQSAsyncClient
                = new AmazonSQSAsyncClient();
        return awsSQSAsyncClient;
    }
}

Here's the test class:

@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
public class SqsQueueDaoIT {

    private static final Logger LOGGER = LoggerFactory.getLogger(SqsQueueDaoIT.class);

    @Autowired
    SqsQueueDao sqsQueueDao;
    @Autowired
    AmazonSQSAsync amazonSQSAsync;

    final String TEST_QUEUE_NAME = "queueName";
    final String TEST_QUEUE_ENDPOINT = "sqs.us-east-1.amazonaws.com/123123123123123/";
    final String TEST_ACTION = "validate";
    final String TEST_BODY = "new body";

    @Test
    public void helloTest() {
        sqsQueueDao.send(TEST_QUEUE_NAME, TEST_ACTION, TEST_BODY);
        String body = receiveMessageResult().getMessages().get(0).getBody();
        assertTrue(body.contains("action"));
        assertTrue(body.contains("source_system_id"));
        assertTrue(body.contains(TEST_ACTION));
        assertTrue(body.contains(TEST_BODY));
    }

    private ReceiveMessageResult receiveMessageResult() {
        ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(TEST_QUEUE_ENDPOINT + TEST_QUEUE_NAME);
        receiveMessageRequest.putCustomQueryParameter("QueueName", TEST_QUEUE_NAME);
        receiveMessageRequest.putCustomQueryParameter("QueueUrl", TEST_QUEUE_ENDPOINT);
        return amazonSQSAsync.receiveMessage(receiveMessageRequest);
    }


}

The error that prevents me from running the test is:

Failed to instantiate [com.amazonaws.auth.profile.ProfilesConfigFile]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: AWS credential profiles file not found in the given path: /Users/user.name/Code/fullfillment/default

The default for the bean is a different location, and there is no where that I'm setting it to look where it is.

  1. If you have specific knowledge of AWS w/ Spring, is there something common happening?
  2. How do you simply replicate running the application exactly how spring-boot:run does in order to run tests?
Joe Essey
  • 3,457
  • 8
  • 40
  • 69

1 Answers1

0

I don't understand why, but explicitly initializing a new ProfileCredentialsProvider and injecting it into the AmazonSQSAsyncClient made the configuration work in testing.

public AmazonSQSAsyncClient amazonSQSClient() {
     ProfileCredentialsProvider profileCredentialsProvider = new ProfileCredentialsProvider();
            AmazonSQSAsyncClient awsSQSAsyncClient
                    = new AmazonSQSAsyncClient(profileCredentialsProvider);
            return awsSQSAsyncClient;
}
Joe Essey
  • 3,457
  • 8
  • 40
  • 69