简体   繁体   中英

How do you handle (or block) the sending of emails during integration testing within a Spring application?

Perhaps I am not using the right search keywords but I cannot find anything on the subject of emails within integration tests. The word "mail" doesn't even appear once in the docs on Testing .

I would like to know if it is even possible to check if a Spring application is trying to send an email, and more importantly, how do I simulate it or block it from actually trying to contact an SMTP server every time I run my integration tests?

This wouldn't be much of a SO question without any code, so here's what I've got:

@Test
public void testResendConfirmationEmail() throws Exception {
    mockMvc.perform(get("/resend").principal(getSpoofedPrincipal()))
            .andExpect(status().isFound())
            .andExpect(redirectedUrl("/notactive"));
}

As you can see, I'm using Spring's MockMVC as the main entry point for my integration tests, in this case the resending of a confirmation email.

So how do I:

  1. Block it from sending any emails?
  2. Check that it sent the right email?

Any advice is greatly appreciated!

Maybe you could set up a fake email server using Dumpster . Then you could send it, confirm that it arrives without actually requiring a recipient.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM