jeudi 1 février 2018

How to perform unit test for email sender with Mockito

I have a method that sends email on order confirmation

 public MimeMessagePreparator constructOrderConfirmationEmail(User user, Order order, Locale locale) {
        Context context = new Context();

        context.setVariable("order", order);
        context.setVariable("user", user);
        context.setVariable("cartItemList", order.getCartItemList());
        String text = templateEngine.process("orderConfirmationEmailTemplate", context);

        MimeMessagePreparator messagePreparator = new MimeMessagePreparator() {
            @Override
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper email = new MimeMessageHelper(mimeMessage);
                email.setTo(user.getEmail());
                email.setSubject("Order Confirmation - " + order.getId());
                email.setText(text, true);
                email.setFrom(new InternetAddress("ezuguValentine@gmail.com"));
            }
        };
        return messagePreparator;
    }

so far this is what I have but I get a null pointer at the when method

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MailConstructor.class)
public class MailConstructorTest {

    @Autowired
    MailConstructor mailConstructor;

    @MockBean
    private TemplateEngine templateEngine;

    @MockBean
    private Environment env;

 @Test
   public void constructOrderConfirmationEmailTest() throws Exception {

    Context context = new Context();
    Order order = new Order();
    order.setId(1L);
    Locale locale = new Locale("1", "3", "4");
    User user = new User();
    user.setEmail("valentineezugu@mail.ru");
    user.setId(1L);

    context.setVariable("order", order);
    context.setVariable("user", user);
    context.setVariable("cartItemList", order.getCartItemList());

    String text =new String() ;

    when(templateEngine.process(Matchers.anyString(), context)).thenReturn(null);

    MimeMessagePreparator messagePreparator = new MimeMessagePreparator() {

        @Override
        public void prepare(MimeMessage mimeMessage) throws Exception {
            MimeMessageHelper email = new MimeMessageHelper(mimeMessage);
            email.setTo(user.getEmail());
            email.setSubject("Order Confirmation - " + order.getId());
            email.setText(text, false);
            email.setFrom(new InternetAddress("ezuguValentine@gmail.com"));
        }
    };

    mailConstructor.constructOrderConfirmationEmail(user, order, locale.ENGLISH);

    Mockito.verify(templateEngine).process(Matchers.anyString(), context);
}

My test is probably totally wrong I just don't know how to go about it when it requires a template ,any other suggestion or may testing not with Mockito would be helpful

rg.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Misplaced argument matcher detected here:

-> at com.valentine.utility.MailConstructorTest.constructOrderConfirmationEmailTest(MailConstructorTest.java:58)

Aucun commentaire:

Enregistrer un commentaire