电子邮件模拟失败

时间:2018-04-20 05:15:46

标签: java junit mockito

我试图模拟发送电子邮件功能但遇到以下错误。我做错了什么或遗失了什么。

@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;
  }

错误:         java.lang.AssertionError:         传递给when()不是模拟的参数!         正确存根的示例:         doThrow(new RuntimeException())。when(mock).someMethod();         在org.junit.Assert.fail(Assert.java:88)         在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)         在

  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)

2 个答案:

答案 0 :(得分:0)

错误消息试图说Mockito.when()方法只接受模拟/间谍对象。

您在mailService方法中使用when,这是错误的,因为它既没有被嘲弄也没有被抄袭。

mailService=new MailServiceImpl(javaMailSender);

由于未提供测试代码,因此无法详细说明。

但你可以尝试更换
  mailService=new MailServiceImpl(javaMailSender); 同   mailService=Mockito.spy(new MailServiceImpl(javaMailSender));

这会使你的mailService成为mockito接受的东西。

答案 1 :(得分:0)

删除doNothing()。when(mailService).sendMail(getEmailProperties());解决了这个问题。不确定导致此问题的原因