Today, I released a new library to help developers to write integration tests with mail servers.

The library has been released on GitHub and Maven Central.

fakesmtp-junit-runner

Build Status

Coverage Status

Links : github.

Important : Part of the source code of this library has been modified and adapted from the project of FakeSmtp. I want to thank him since his project inspired me the creation of that library.

This library is an extension to JUnit to allow developers to write integration tests where a SMTP server is required.

The how-to is quite simple :

  • Inserts the @Rule in your integration tests
  • a Fake SMTP Server will start
  • You can send mails on it
  • You can control the mailbox
  • Write your own assertions to check mails.

Installation

The project requires JUnit 4.11 or higher. It also requires SLF4J API presents in the classpath. I did not bundle them in the library to avoid conflicts.

To use it, adds the library to your maven or gradle config script :

For maven :

1
2
3
4
5
6
<dependency>
<groupId>com.github.sleroy</groupId>
<artifactId>fakesmtp-junit-runner</artifactId>
<version>0.1.1</version>
<scope>test</scope>
</dependency>

For gradle :

1
testCompile "com.github.sleroy:fakesmtp-junit-runner:0.1.1"

Usage

Step 1 :

Creates a JUnit test :

1
2
3
4
5
6
7
8
9
public class SmtpSendingClassTest {
@Test
public void testCase1() {
}
}

Step 2 :

Adds the new Junit rule with its configuration :

1
2
3
4
5
6
7
8
9
10
11
public class SmtpSendingClassTest {
@Rule
public FakeSmtpRule smtpServer = new FakeSmtpRule(ServerConfiguration.create().port(2525).charset("UTF-8"));
@Test
public void testCase1() {
}
}

Step 3 :

You are ready to use it, controls the mailbox or the server state :

1
Assert.assertTrue(smtpServer.isRunning());
1
2
3
4
5
6
7
8
9
10
11
12
public class SmtpSendingClassTest {
@Rule
public FakeSmtpRule smtpServer = new FakeSmtpRule(ServerConfiguration.create().port(2525).charset("UTF-8"));
@Test
public void testCase1() {
Assert.assertTrue(smtpServer.isRunning());
Assert.assertTrue(smtpServer.mailbox().isEmpty());
}
}