2

I want to write unit tests for JavaMail. I'm already using prepared outlook server. So my question is what is the best way to write unit tests: I mean: what should I check?

  1. check if only my program send a letter (call the appropriate method like Transport.send(msg))
  2. or check if letter is received by addressee?
VB_
  • 43,322
  • 32
  • 111
  • 238

2 Answers2

5

Unit Tests are simple tests without relation to other systems. Integration Test can verify cooperation with other server like Outlook.

In Unit Test you should mock mail server and test only your own code. You are responsible for your code only. For simplicity mock mail server, emulate it behaviour (mock) and test corner cases.

Read this: What's the difference between unit tests and integration tests?

Community
  • 1
  • 1
MariuszS
  • 27,972
  • 12
  • 103
  • 144
4

For unittests (as MariuszS says) you should not connect to a real system. Have a look at JavaMail Mock2 https://github.com/salyh/javamail-mock2 ,this is designed for doing unittests with JavaMail and does not need a real system.

Its primarily focused on IMAP/POP3 but SMTP Mock is also available. Its available in maven central.

Features

  • Support imap, imaps, pop3, pop3s, smtp, smtps
  • Supported for POP3: cast to POP3Folder, Folder.getUID(Message msg)
  • Supported for IMAP: cast to IMAPFolder, cast to UIDFolder, Subfolders, -Folder.getMessagesByUID(...), delete/rename folders, append messages
  • Support for SMTP: Mock Transport.send()

Unsupported for the moment: IMAP extensions like IDLE, CONDSTORE, ... and casts to POP3Message/IMAPMessage, store listeners

salyh
  • 1,919
  • 1
  • 13
  • 30