Search code examples
javaunit-testingpluginsplayframeworkmail-server

How to use GreenMail test suite in play framework?


Is it possible to use GreenMail test suite in Play projects?

If so, how to use it?

Is there any plugin? (I found a GreenMail plugin for Grails but none for Play)

Or does Play support mail server testing by itself without GreenMail?

(I'm using Eclipse IDE by the way)


Solution

  • If you can use it with JUnit, you can use it with Play. The code below is just a regular play unit test:

    public class GreenMailTest extends UnitTest {
    
      private GreenMail mail;
    
      @Before
      public void startGreenMail() {
        GreenMail greenMail = new GreenMail();
        greenMail.start();  
      }
    
      @Test
      public void myTestThatUsesGreenMail() {
        GreenMailUtil.sendTextEmailTest("to@localhost.com", "from@localhost.com", "subject", "body");
        assertEquals("body", GreenMailUtil.getBody(greenMail.getReceivedMessages()[0]));
      }
    
      @After
      public void stopGreenMail() {
        greenMail.stop();
      }
    
    }
    

    Hope that helps