-1

While using Spock i can do something like this:

when:
12.times {mailSender.send("blabla", "subject", "content")}

then:
12 * javaMailSender.send(_)

When i tried to do same in Mockito:

verify(javaMailSender,times(12)).send(any(SimpleMailMessage.class))

I got an error that SimpleMailMessage has null values, so i had to initialize it in test:

SimpleMailMessage simpleMailMessage = new SimpleMailMessage()
simpleMailMessage.setTo("blablabla")
simpleMailMessage.subject = "subject"
simpleMailMessage.text = "content"
verify(javaMailSender,times(12)).send(simpleMailMessage))

Now it works but it's a large workload and i really don't care about equality. What if SimpleMailMessage will have much more arguments or another objects with another arguments, meh. Is there any way to check that send method was just called X times?

EDIT: added implementation of send method.

private fun sendEmail(recipient: String, subject: String, content: String)
{
    val mailMessage = SimpleMailMessage()

    mailMessage.setTo(recipient)
    mailMessage.subject = subject
    mailMessage.text = content

    javaMailSender.send(mailMessage)
}

There are 2 senders, mailSender is my custom object and javaMailSender is from another libary

Stacktrace:

Mockito.verify(javaMailSender, 
Mockito.times(2)).send(Mockito.any(SimpleMailMessage.class))
    |      |                       |         |            |
    |      |                       |         |            null
    |      |                       |         Wanted but not invoked:
    |      |                       |         javaMailSender.send(
    |      |                       |             <any org.springframework.mail.SimpleMailMessage>
    |      |                       |         );
    |      |                       |         -> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    |      |                       |          
    |      |                       |         However, there were exactly 2 interactions with this mock:
    |      |                       |         javaMailSender.send(
    |      |                       |             SimpleMailMessage: from=null; replyTo=null; to=blabla; cc=; bcc=; sentDate=null; subject=subject; text=content
    |      |                       |         );
    |      |                       |         -> at MailSenderServiceImpl.sendEmail(MailSenderServiceImpl.kt:42)
    |      |                       |          
    |      |                       |         javaMailSender.send(
    |      |                       |             SimpleMailMessage: from=null; replyTo=null; to=blabla; cc=; bcc=; sentDate=null; subject=subject; text=content
    |      |                       |         );
kriegaex
  • 63,017
  • 15
  • 111
  • 202
minizibi
  • 653
  • 3
  • 13
  • 28
  • What are the parameters of `send`? In the first example, you have shown three strings while in the second you have an object – Thiyagu Feb 22 '18 at 16:42
  • parameter is SimpleMailMessage. method mailSender.send(String, String, String) internally initialize SimpleMailMessage in the way as in 2nd example. – minizibi Feb 22 '18 at 16:45
  • I don't see a problem with that code fragment. Can you give more details as to where it said it had null values? – Thiyagu Feb 22 '18 at 16:47
  • added stacktrace, as you see. It need the equality SimpleMailMessage but i just want to check: "did method was called 12 times?" I dont care for equality of SimpleMailMessage – minizibi Feb 22 '18 at 16:51
  • 1
    Could you please share an [MCVE](http://stackoverflow.com/help/mcve) instead of just an set of incoherent snippets? E.g. in your test I do not see the equivalent to `mailSender.send("blabla", "subject", "content")`. BTW, I removed the _spock_ tag because this definitely is no Spock question. – kriegaex Feb 23 '18 at 00:50
  • I think the problem was Groovy. ArgumentMatchers.any(Class type) method doesn't works when i put there SimpleMailMessage.class written in Groovy. But when I did it in Java file it works. – minizibi Feb 23 '18 at 10:40

1 Answers1

0

If you don't care for the parameter of send, leave any() empty:

verify(javaMailSender,times(12)).send(any())
ndueck
  • 713
  • 1
  • 8
  • 27
  • and do it in Spock (Groovy) and it won't work. No idea why : / – minizibi Feb 23 '18 at 21:04
  • so you combine spock and mockito? could you provide the mockito version you're using? i'd like to check it – ndueck Feb 23 '18 at 21:08
  • yeah because Mockito (with a plugin) is able to mock final classes and half of my project is in Kotlin (which has final as default). So when I need to mock Kotlin class then using Mockito. I have mockito-core 2.11.0 as dependency – minizibi Feb 23 '18 at 21:20