Home > Software engineering >  Mockito - verify a method call with expected argument but ignore certain fields
Mockito - verify a method call with expected argument but ignore certain fields

Time:10-29

I am mocking a class called EmailSender which has a method sendEmail() that takes in an argument of class Email. I am trying to verify that my mockEmailSender will call the sendEmail function once with the expected Email class as such: verify(mockEmailSender, times(1)).sendEmail(expectedEmail)

class Email {
   String title;
   List<String> receipients;
   EmailBody body;
}

I am initializing the expected Email object like the follow Email expectedEmail = Email.builder().setRecipients(//example list).setTitle("Test title").setBody(//make this not matter)

How can I make this work while ignoring the body field? So I just want to check that the sendEmail() method is called using expectedEmail and that the expectedEmail has all of the fields as set by me while ignoring certain fields such as the body field that I do not wish to explicitly set.

CodePudding user response:

The ArgumentMatchers.refEq matcher can help you easily ignore fields you don't care about:

verify(mockEmailSender, times(1)).sendEmail(refEq(expectedEmail, "body"));
  • Related