REST Assured sign-up email test in Java
Many sign-up flows can be tested without a browser. The app has a JSON endpoint that creates the account and sends an email, and another that accepts the code or the link. This guide tests that flow in Java with REST Assured and JUnit 5, and uses the single-file Java client to wait for the email. The test finishes with a call that proves the account is verified.
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, so tests running in parallel never read each other's mail.
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 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. Add io.rest-assured:rest-assured and org.junit.jupiter:junit-jupiter as test dependencies.
The test
Save this as src/test/java/SignUpApiTest.java. The routes /api/signup, /api/verify and /api/me and the JSON field names are placeholders for your own API. Use the test only against an app you own, preferably a staging environment.
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import java.util.Map;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
class SignUpApiTest {
final AssertInbox inbox = new AssertInbox(System.getenv("ASSERTINBOX_KEY"), System.getenv("ASSERTINBOX_NAMESPACE"));
final String tag = inbox.uniqueTag("api");
@BeforeAll
static void configure() {
RestAssured.baseURI = System.getenv().getOrDefault("APP_URL", "http://localhost:3000");
}
@AfterEach
void cleanUp() {
inbox.deleteMessages(tag);
}
String signUp() {
String email = inbox.address(tag);
given().contentType(ContentType.JSON)
.body(Map.of("email", email, "password", "correct-horse-battery-staple"))
.post("/api/signup")
.then().statusCode(201);
return email;
}
@Test
void verifyWithCode() {
String email = signUp();
String code = inbox.waitForOtp(tag, Map.of("subject", "verify"));
given().contentType(ContentType.JSON)
.body(Map.of("email", email, "code", code))
.post("/api/verify")
.then().statusCode(200);
given().auth().basic(email, "correct-horse-battery-staple")
.get("/api/me")
.then().statusCode(200).body("verified", equalTo(true));
}
@Test
void verifyWithLink() {
String email = signUp();
String link = inbox.waitForLink(tag, Map.of("subject", "verify"));
assertNotNull(link);
given().redirects().follow(true).get(link).then().statusCode(200);
given().auth().basic(email, "correct-horse-battery-staple")
.get("/api/me")
.then().statusCode(200).body("verified", equalTo(true));
}
@Test
void wrongCodeIsRejected() {
String email = signUp();
inbox.waitFor(tag, Map.of("subject", "verify"));
given().contentType(ContentType.JSON)
.body(Map.of("email", email, "code", "000000"))
.post("/api/verify")
.then().statusCode(400);
}
}
Run it with mvn test or gradle test. Nothing in it starts a browser, so the whole class runs in a few seconds once the email arrives.
Why wait before the second call
REST Assured calls return as soon as the server answers, but the email is sent by a queue or a background job and arrives later. The order of every test is the same: trigger the email, wait for it, then act on what it contained. waitFor is one call that returns when the message arrives, so the test needs no polling loop or sleep. It retries a 408 timeout up to two more times by default; pass "retries" and "timeout" in the filter map to change that.
Check the link before you follow it
Following the link with get(link) is the same thing a user does, but a link can be wrong in ways the response hides. Before following it, assert that its host is the one you expect, for example assertTrue(link.startsWith(RestAssured.baseURI)). The guide on checking every link in an email covers the rest.
Parallel runs and a second email
Each test builds its own tag, so JUnit 5's parallel execution (junit.jupiter.execution.parallel.enabled=true) is safe. If a test triggers a second email to the same address, such as a resend, pass the id of the first message as after: inbox.waitFor(tag, Map.of("after", first.id)) returns only a newer message.
Troubleshooting
- AssertInboxException with code timeout. The app never sent the email, or sent it to another address. Print
inbox.address(tag)and compare it with the app's logs. - Code unauthorized, status 401. The key is wrong or missing in the environment that runs the tests.
- HTTP 429. The key used its 2,000 requests for the day.
GET /v1/meshows usage. - IllegalStateException about a missing code. The email has no recognisable code. Read the message text and parse it yourself; the full body is always returned.
- The sign-up call returns 429. Your own app rate-limits sign-ups. Lower the test parallelism rather than reusing addresses.
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.