Get EasyMock to throw an exception during a Retry call
Clash Royale CLAN TAG#URR8PPP
Get EasyMock to throw an exception during a Retry call
I am attempting to write a unit test which throws an exception on a RetryTemplate that I have mocked. The current test fails on my assertion.
/**
* Test maybeSendNotification() IOException.
*/
@Test(expected = RuntimeException.class)
public void testMaybeSendNotificationIOException() throws IOException
Instance instance = new Instance();
instance.setState(new InstanceState().withName("DOWN"));
instance.setKeyName("prod");
EasyMock.expect(slackMessageSender.send(EasyMock.isA(HashMap.class))).andThrow(new RuntimeException());
EasyMock.replay(slackMessageSender);
assertFalse(instanceService.maybeSendNotification(instance));
EasyMock.verify(slackMessageSender);
slackMessageSender
and retryTemplate
are both mocks.
slackMessageSender
retryTemplate
This is the method under test:
boolean maybeSendNotification(Instance newInstance)
{
Map<String, String> context = new HashMap<String, String>();
context.put("message", format("Instance with ID '%s' for load balancer '%s' status is DOWN.",
newInstance.getInstanceId(),
newInstance.getKeyName()));
try
retryTemplate.execute( c -> slackMessageSender.send(context));
LOG.debug(String.format("Successfully sent Slack notification for instance '%s'.", newInstance.getInstanceId()));
return true;
catch(IOException e)
LOG.debug(String.format("Failed to send Slack notification for instance '%s'.", newInstance.getInstanceId()));
return false;
Currently the method returns true
but I would like to get it to throw the IOException and return false. How do I mock this behaviour?
true
2 Answers
2
I don't know what the retry template is doing but the code seems alright. However, you seem to want to do EasyMock.expect(slackMessageSender.send(EasyMock.isA(HashMap.class))).andThrow(new IOException());
no?
EasyMock.expect(slackMessageSender.send(EasyMock.isA(HashMap.class))).andThrow(new IOException());
And if you want to throw an exception for every retry, you will want EasyMock.expect(slackMessageSender.send(EasyMock.isA(HashMap.class))).andStubThrow(new IOException());
EasyMock.expect(slackMessageSender.send(EasyMock.isA(HashMap.class))).andStubThrow(new IOException());
As you are saying that retryTemplate
is also a mock I would assume that currently the slackMessageSender.send
method is never executed in the test because the callback of retryTemplate.execute
is not called.
I think you need to setup the retryTemplate mock to execute its parameter. Something like:
retryTemplate
slackMessageSender.send
retryTemplate.execute
EasyMock.expect(retryTemplate.execute).andAnswer(() ->
final RetryCallback retryCallback = (RetryCallback) getCurrentArguments()[0];
return retryCallback.doWithRetry(context);
);
EasyMock.replay(retryTemplate);
Also note that with @Test(expected = RuntimeException.class)
the line EasyMock.verify(slackMessageSender);
would never be executed and the slackMessageSender
mock never verified because the code will exit when the exception is thrown.
@Test(expected = RuntimeException.class)
EasyMock.verify(slackMessageSender);
slackMessageSender
With jUnit 5 you would be able to do something like:
EasyMock.replay(slackMessageSender);
IOException exception = assertThrows(IOException.class, () ->
assertFalse(instanceService.maybeSendNotification(instance));
);
EasyMock.verify(slackMessageSender);
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.