LoginSignup
3
3

More than 3 years have passed since last update.

Java tips GreenMail をJunit5に導入する

Posted at

GreenMail をJunit5に導入する

単体テスト用のメール通信ライブラリ「GreenMail」を導入しようと思って調べてみると、
Junit4の@Ruleを使用したサンプルが多くて、JUnit5でどうすればいいのか少し悩んだので
備忘録としてまとめます。

サンプルソース

Junit5でGreenMailを使用して問題なくSMTP通信できた方法は以下のような書き方です。

class SampleTest {

  // SMTP通信クラス(仮想サーバみたいなもの)
  private GreenMail smtp =
    new GreenMail(new ServerSetup(3025,"localhost",ServerSetup.PROTOCOL_SMTP));

  @BeforeEach
  void before() {
    // 仮想サーバ開始
    smtp.start();
  }

  @AfterEach
  void after() {
    // 仮想サーバ終了
    smtp.stop();
  }
}

自分でローカルにメールサーバを立ち上げて、開始と終了をソースで明示的に書くようなイメージでしょうか。

まとめ

JUnit5でも問題なくGreenMailを使えます。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3