Reputation: 442
I have a similar piece of code for reading all the messages from a SQS queue
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl);
receiveMessageRequest.withMaxNumberOfMessages(10);
List<Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
while (messages.size() > 0) {
for (final Message message : messages) {
System.out.println("Message");
System.out.println(" MessageId: " + message.getMessageId());
System.out.println(" ReceiptHandle: " + message.getReceiptHandle());
System.out.println(" MD5OfBody: " + message.getMD5OfBody());
System.out.println(" Body: " + message.getBody());
for (final Entry<String, String> entry : message.getAttributes().entrySet()) {
System.out.println("Attribute");
System.out.println(" Name: " + entry.getKey());
System.out.println(" Value: " + entry.getValue());
}
}
receiveMessageRequest = new ReceiveMessageRequest(myQueueUrl);
receiveMessageRequest.withMaxNumberOfMessages(10);
messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
}
Link: How to fetch all messages from SQS queue till the queue is empty?
In the unit test cases, I have mocked the message list to list created for the testing
@Mock
ReceiveMessageResult receiveMessageResult;
.
.
.
when(receiveMessageResult.getMessages()).thenReturn(messageList);
I am having problems in testing this since the loop is infinite for a hard-coded list in test cases due to the re-assignation of the variable messages.
Is there a way to decrease this variable only for test cases?
PS: Found similar question to this topic but they did not cover my use case.
Upvotes: 0
Views: 156
Reputation: 176
You can chain thenReturn-calls, like so:
when(receiveMessageResult.getMessages()).thenReturn(messageList).thenReturn(Collections.emptyList());
Upvotes: 1