我试图模拟发送电子邮件功能,该功能在特定工作/任务完成时发送电子邮件。当我尝试对它进行单元测试时,它会低于错误。这是测试它的正确方法还是错误或遗漏了任何东西。
@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("from@Test.com");
emailProperties.setToAddress("To@Test.com");
emailProperties.setEmailBody("Test");
emailProperties.setSubject("Test email");
emailProperties.setAttachment((File) null);
emailProperties.setMessageType(1);
return emailProperties;
}
下面是从testSendEmail方法抛出的错误消息。任何建议或任何其他测试方法。谷歌搜索并没有多大帮助。
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)
错误消息试图说Mockito.when()
方法只接受模拟/监视的对象。
您在when
方法中使用mailService
这是错误的,因为它既不是模拟对象也不是监视对象。
mailService=new MailServiceImpl(javaMailSender);
我不能详细介绍,因为没有提供正在测试的代码。
但是您可以尝试更换
mailService=new MailServiceImpl(javaMailSender);
跟 mailService=Mockito.spy(new MailServiceImpl(javaMailSender));
这应该让你mailService
成为嘲笑者会接受的东西。
删除doNothing((.when(mailService(.sendMail(getEmailProperties(((;修复了此问题。不确定导致此问题的原因