# JUnit email and OTP testing

MailSlurp integrates cleanly with JUnit when the Java test suite owns both the app action and the email assertion. The usual pattern is to create an inbox with the Java SDK, submit the generated address in the workflow under test, wait for the latest email, and extract a code or link from the body.

## Install

Maven:

```xml
<dependency>
  <groupId>com.mailslurp</groupId>
  <artifactId>mailslurp-client-java</artifactId>
  <version>LATEST</version>
  <type>pom</type>
</dependency>
```

Gradle:

```kotlin
dependencies {
    implementation("com.mailslurp:mailslurp-client-java")
}
```

## Create an inbox with the Java SDK

```java
InboxDto inbox = inboxControllerApi.createInboxWithDefaults().execute();
// verify inbox
assertEquals(inbox.getEmailAddress().contains("@mailslurp"), true);
assertNotNull(inbox.getId());
```

## Example JUnit OTP flow

```java
import com.mailslurp.apis.InboxControllerApi;
import com.mailslurp.apis.WaitForControllerApi;
import com.mailslurp.clients.ApiClient;
import com.mailslurp.clients.Configuration;
import com.mailslurp.models.Email;
import com.mailslurp.models.InboxDto;
import org.junit.jupiter.api.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.jupiter.api.Assertions.*;

class SignupOtpTest {

    @Test
    void completesEmailVerification() throws Exception {
        ApiClient defaultClient = Configuration.getDefaultApiClient();
        defaultClient.setApiKey(System.getenv("API_KEY"));
        defaultClient.setConnectTimeout(120_000);
        defaultClient.setReadTimeout(120_000);
        defaultClient.setWriteTimeout(120_000);

        InboxControllerApi inboxControllerApi = new InboxControllerApi(defaultClient);
        WaitForControllerApi waitForControllerApi = new WaitForControllerApi(defaultClient);

        InboxDto inbox = inboxControllerApi.createInboxWithDefaults().execute();

        // Submit inbox.getEmailAddress() in the app under test here.

        Email email =
            waitForControllerApi
                .waitForLatestEmail()
                .inboxId(inbox.getId())
                .timeout(120_000L)
                .unreadOnly(true)
                .execute();

        Matcher matcher = Pattern
            .compile("(?:verification code|OTP|code)[:\\\\s-]*(\\\\d{6})")
            .matcher(email.getBody());

        assertTrue(matcher.find());
        assertNotNull(matcher.group(1));
    }
}
```

## Example projects

- [Gradle JUnit 5 example](https://github.com/mailslurp/examples/tree/master/java-gradle-junit5)
- [Maven JUnit 4 example](https://github.com/mailslurp/examples/tree/master/java-maven-junit4)

<div data-component="DocsExamplesList" class="docs-card-grid docs-examples-list" data-example-terms="junit" data-example-count="2">
<a class="docs-card docs-example-card" href="https://www.github.com/mailslurp/examples/tree/master/java-gradle-junit5" target="_blank" rel="noopener noreferrer"><span class="docs-card-title">Java Gradle Junit5</span><span class="docs-card-description">java example repository: <code>java-gradle-junit5</code></span></a>
<a class="docs-card docs-example-card" href="https://www.github.com/mailslurp/examples/tree/master/java-maven-junit4" target="_blank" rel="noopener noreferrer"><span class="docs-card-title">Java Maven Junit4</span><span class="docs-card-description">java example repository: <code>java-maven-junit4</code></span></a>
</div>

## Related docs

- [Java SDK](/docs/java/)
- [Wait for methods](/docs/wait-for/)
- [Integration testing guide](/docs/testing/)
