How do I mock simpleMessageListenerContainer during the test cases

219 views Asked by At

I am trying to execute few test cases for one of my modules which using AWS SQS for pushing and listening to the messages in the queues. In the real time I make the SQS connection like:

public AmazonSQSAsync getAmazonSQSAsync() {
    return AmazonSQSAsyncClientBuilder.standard().withRegion(Regions.ReginonName)
            .withCredentials(new AWSStaticCredentialsProvider(new BasicSessionCredentials(accessKey, secretKey, sessionToken)))
            .build();
}

and I provide the valid values of all the above keys. This works fine with the keys.

However, when I try to run the Junit tets cases on the CICD server without the keys, I keep getting the following exception.

 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'simpleMessageListenerContainer' defined in class path resource [org/springframework/cloud/aws/messaging/config/annotation/SqsConfiguration.class]: The security token included in the request is invalid. (Service: AmazonSQS; Status Code: 403; Error Code: InvalidClientTokenId; Request ID: 2c866447-fc44-558a-9e93; Proxy: null)

I would like to mock the above bean so that it can be injected without using the real keys. I tried mocking the bean like below, but it doesn't seem to work:

@Mock
private SimpleMessageListenerContainerFactory simpleMessageListenerContainerFactory;

How can I mock this so that, I do not have to put in my real keys and can run my test cases?

1

There are 1 answers

0
Saurabh Tiwari On

In my case, the problem was that the bean for @SQSListener (that I had created for reacting to msgs) was getting created. In the absence of the secret keys this creation was getting failed.

So I simply marked my @SQSListner bean with @Profile(!local) and then it didn't create this bean on local env without the keys.

On all other env, I am creating this bean with STS Assume Role credentials (which do not require explicit keys in the code)