I have the following method in my Java
class:
public class AwsHelper {
private AmazonSQS sqs;
private void sendMessageToQueue(String message){
sqs = AmazonSQSClientBuilder.defaultClient();
SendMessageRequest sendMessageRequest = new SendMessageRequest();
sendMessageRequest.setQueueUrl("");
sendMessageRequest.setMessageBody(message);
sendMessageRequest.setMessageGroupId("");
sqs.sendMessage(sendMessageRequest);
}
I want to be able to mock the behavior of sqs.sendMessage(sendMessageRequest);
so that my unit test does not send a message to a queue.
I have tried to do so as follows in my test class, but sqs
actually sends a message to the queue when my test is executed. Assume this is due to being assigned by AmazonSQSClientBuilder.defaultClient()
.
How can I solve this?
public class AwsSQSReferralsUtilTest {
@Spy
@InjectMocks
private AwsHelper awsHelper;
@Mock
AmazonSQS sqs;
@BeforeClass
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@AfterMethod
public void afterEachMethod() {
Mockito.reset(awsHelper);
}
@Test
public void shouldSendMessage() {
Mockito.when((sqs.sendMessage(any(SendMessageRequest.class)))).thenReturn(new SendMessageResult());
awsHelper.sendMessageToQueue("");
}
}