AssertInbox

Spring Boot email verification test with a real inbox

A Spring Boot app that registers users usually sends a verification email through JavaMailSender. A mock mail sender proves that your code called send; it does not prove that a real SMTP connection, a real template and a real link work together. This guide runs an end-to-end test with @SpringBootTest: the app sends through real SMTP settings to an AssertInbox address, and the test waits for the message with the single-file Java client, follows the verification link and asserts the user is enabled.

When a real inbox beats a mock

Use a mock or an in-process SMTP server for fast unit tests of one class. Use a real inbox for a small number of end-to-end tests, because only those catch a wrong base URL in a link, a template that renders a placeholder, a sender address the mail server refuses, or a TLS setting that fails. Keep the end-to-end set small and run it against a staging mail account that you own.

How it works

Mail sent to <namespace>.<tag>@assertinbox.com is stored for your key. GET /v1/wait long-polls for up to 25 seconds and returns the newest matching message as JSON, with otp, magic_link and links already extracted. Messages are deleted after 24 hours. A unique tag per test is a private inbox.

Get an API key and the client file

Get a key in a click at /start. One request creates a key and your private namespace, with no email address or card. The key is shown once, so keep it in your CI secret store and export ASSERTINBOX_KEY (the api_key) and ASSERTINBOX_NAMESPACE (the namespace). Use inboxes only for services you own or control; see the terms. Limits and error codes are on the limits page.

curl -o src/test/java/AssertInbox.java https://assertinbox.com/sdk/AssertInbox.java

The client needs Java 11 or later and nothing else, so it sits next to Spring Boot's own test starter without adding a dependency.

A test profile with real SMTP settings

Put the mail settings of your staging mail service in src/test/resources/application-e2e.properties and read the secrets from environment variables, never from the file. The names below are the standard Spring Boot mail properties; the host, port and credentials are yours.

spring.mail.host=${SMTP_HOST}
spring.mail.port=${SMTP_PORT:587}
spring.mail.username=${SMTP_USER}
spring.mail.password=${SMTP_PASSWORD}
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
app.base-url=http://localhost:${local.server.port}

The last line matters: if your verification link is built from a configured base URL, it must point at the port the test server picked, or following the link in the test will fail.

The test

Save this as src/test/java/RegistrationEmailE2ETest.java. The routes /register and /verify, the UserRepository and its enabled flag are placeholders for your own application. The test follows the link with TestRestTemplate; the link points at the same server because of the profile above.

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Map;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("e2e")
class RegistrationEmailE2ETest {
    final AssertInbox inbox = new AssertInbox(System.getenv("ASSERTINBOX_KEY"), System.getenv("ASSERTINBOX_NAMESPACE"));
    final String tag = inbox.uniqueTag("spring");

    @Autowired TestRestTemplate http;
    @Autowired UserRepository users;

    @AfterEach
    void cleanUp() {
        inbox.deleteMessages(tag);
    }

    @Test
    void registrationSendsALinkThatEnablesTheUser() {
        String email = inbox.address(tag);

        ResponseEntity<String> registered = http.postForEntity("/register", Map.of("email", email, "password", "correct-horse-battery-staple"), String.class);
        assertEquals(HttpStatus.CREATED, registered.getStatusCode());
        assertFalse(users.findByEmail(email).orElseThrow().isEnabled(), "must not be enabled before the link is used");

        AssertInbox.Message mail = inbox.waitFor(tag, Map.of("subject", "verify"));
        assertTrue(mail.links.stream().noneMatch(l -> l.contains("localhost:3000")), "link built from the wrong base URL");
        String link = inbox.waitForLink(tag, Map.of("subject", "verify"));

        ResponseEntity<String> verified = http.getForEntity(link, String.class);
        assertTrue(verified.getStatusCode().is2xxSuccessful(), verified.getStatusCode().toString());
        assertTrue(users.findByEmail(email).orElseThrow().isEnabled());
    }
}

Run it with mvn test -Dspring.profiles.active=e2e or the Gradle equivalent in a separate CI job, so a missing key does not break the fast unit-test run. When the environment variables are not set, new AssertInbox throws at once with a clear message.

Checking the content, not only the link

The Message object has subject, from, to and text as well as the link. Assert the sender address your mail server is configured to use, that the subject is the one the template defines, and that the text contains the user's name and no unrendered placeholders such as ${ or null. Those are the template mistakes that a mock cannot see.

Waiting and retries

Registration often sends the email from an asynchronous method or a queue, so the message can arrive after the HTTP response. waitFor is one call that returns as soon as the message arrives; it retries a 408 timeout up to two more times by default. Pass "retries" and "timeout" in the filter map for a slower queue. Avoid Thread.sleep in the test.

Troubleshooting

Next steps

For unique tags, the trigger-then-wait order, retries and cleanup in one place, read Reliable email tests. More stacks are on the guides page.