Issue
Im trying to mock the send email functionality which sends out email when a particular job/task is completed. When i try to unit test it getting below error. Is this the right way to test it or something wrong or missing anything.
@Mock
private MailServiceImpl mailService;
@Mock
private JavaMailSenderImpl javaMailSender;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mailService=new MailServiceImpl(javaMailSender);
}
@Test
public void testSendEmail() {
try {
doNothing().when(mailService).sendMail(getEmailProperties());
mailService.sendMail(getEmailProperties());
verify(mailService,times(1)).sendMail(getEmailProperties());
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
public EmailProperties getEmailProperties() {
EmailProperties emailProperties = new EmailProperties();
emailProperties.setFromAddress("[email protected]");
emailProperties.setToAddress("[email protected]");
emailProperties.setEmailBody("Test");
emailProperties.setSubject("Test email");
emailProperties.setAttachment((File) null);
emailProperties.setMessageType(1);
return emailProperties;
}
Below is the error message is thrown from testSendEmail method. Any suggestions or any other ways to test it. google search didn't help much.
java.lang.AssertionError:
Argument passed to when() is not a mock!
Example of correct stubbing:
doThrow(new RuntimeException()).when(mock).someMethod();
at org.junit.Assert.fail(Assert.java:88)
at com.xx.xx.xx.mail.MailServiceTest.testSendEmail(MailServiceTest.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Solution
removal of doNothing().when(mailService).sendMail(getEmailProperties()); fixed the issue. Not sure what was causing this issue
Answered By - NKR