2024-03-19

Release of FakeSmtp-junit-runner

https://sylvainleroy.com/wp-admin/options-general.php?page=ad-inserter.php#tab-2

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

The library has been released on GitHub and Maven Central.

fake SMTP--runner

Build Status

Coverage Status

Links: Github.

Important: Part of the 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 create that library.

This library is an extension to to allow developers to write integration tests an 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 4.11 or higher. It also requires SLF4J API present 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 :

<dependency>
  <groupId>com.github.sleroy</groupId>
  <artifactId>fakesmtp--runner</artifactId>
  <version>0.1.1</version>
  <scope>test</scope>
</dependency>

For Gradle :

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

Usage

Step 1 :

Creates a test :

public class SmtpSendingClassTest {


  @Test
  public void testCase1() {

  }

}

Step 2 :

Adds the new rule with its configuration :

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 :

Assert.assertTrue(smtpServer.isRunning());
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());
  }

}

Sylvain Leroy

Senior Software Quality Manager and Solution Architect in Switzerland, I have previously created my own company, Tocea, in Software Quality Assurance. Now I am offering my knowledge and services in a small IT Consulting company : Byoskill and a website www.byoskill.com Currently living in Lausanne (CH)

View all posts by Sylvain Leroy →