Mocking javax.mail.Transport

Without creating another class that I can inject. Is it possible to mock javax.mail.Transport so I can do some mock testing of the Transport.send() method on Java EE7?

Following up on Bill Shanon's solution since Dumbster does not have a currently working Maven Central artifact, I used GreenMail.

Then I used the following code

final GreenMail mailServer = new GreenMail();
mailServer.start();

final Properties mailSessionProperties = new Properties();
mailSessionProperties.put("mail.smtp.port", String.valueOf(mailServer.getSmtp().getPort()));

final javax.mail.Session mailSession = javax.mail.Session.getInstance(mailSessionProperties);

testObject.setMailSession(mailSession);

That way testObject does not need to change even if it has a static call to Transport.send(message)

How to mock javax.mail.Transport.send() method without actually , Here is an example test, using JMockit: @Test public void checkEmailIsSent(@ Mocked Transport emailTransport) { // Call the code under test  Uses of Transport in javax.mail. Get a Transport object that implements this user's desired Transport protcol. Get a Transport object that can transport a Message of the specified address type. Get an instance of the transport specified in the Provider. Get a Transport object that implements the specified protocol.

If what you want is to not call Transport.send while testing then you could use this:

@RunWith(PowerMockRunner.class)
@PrepareForTest(javax.mail.Transport.class)
public class MyClassTest {
    @Before
    public void before() throws Exception {
        suppress(method(Transport.class, "send", Message.class));
    }
}

see also https://groups.google.com/forum/#!topic/powermock/r26PLsrcxyA

Mocking javax.mail.Transport - javamail - html, Without creating another class that I can inject. Is it possible to mock javax.mail. Transport so I can do some mock testing of the Transport.send() method on Java   public abstract class Transport extends Service. An abstract class that models a message transport. Subclasses provide actual implementations. Note that Transport extends the Service class, which provides many common methods for naming transports, connecting to transports, and listening to connection events.

Yes. On way is to use Mockito. https://code.google.com/p/mockito/

public class CategoryDAOTest {

    private Transport transport = Mockito.mock(Transport.class);

    @Test
    public void sendMessage() throws MessagingException {
        transport.sendMessage(null, null);
    }
}

Or, using annotations to create the mock:

@RunWith(MockitoJUnitRunner.class)
public class CategoryDAOTest {

    @Mock
    private Transport transport;

    @Test
    public void sendMessage() throws MessagingException {
        transport.sendMessage(null, null);
    }
}  

TestSMTPMailServer xref, PowerMockRunner; 26 import test.mock.mail.server. Transport; 35 import javax .mail. thenReturn(false); 79 mockTransport = mock(Transport.class); 80  An SMTP protocol provider for the JavaMail API that provides access to an SMTP server.

Another approach that hasn't been discussed is to wrap calls to Transport.send(message) in a class that delegates the call. It's a bit of overhead in your code base, but it allows you to mock your delegating class and create the outcomes you want.

public class TransportDelegator {
    public void send(Message msg) throws MessagingException {
        Transport.send(msg)
    }
}

Then mock TransportDelegator as you would in whatever framework you are using.

How I learned to love mocks - Matteo Vaccari, Learn how mocks and adapters work together to make working with external The key API call here is Transport.send(message) , which is If I misunderstood the details of how to invoke the Java Mail API, the test could be  Transport (Java (TM) EE 7 Specification APIs) java.lang.Object. javax.mail.Service. javax.mail.Transport. public abstract class Transport extends Service. An abstract class that models a message transport. Subclasses provide actual implementations. Note that Transport extends the Service class, which provides many common methods for naming transports, connecting to transports, and listening to connection events.

I created a new wrapper class and Mocked it with Mockito. This prevented me from adding PowerMock to my project.

public class MyTransport{
  public void send(MineMessage msg) throws MessagingException{
    javax.mail.Transport.send(msg);
  }
}

In side test class we just need

@Mock
private MyTransport myTransport

This will do nothing when is called. You may also want to verifiy myTransport is getting called.

Mockito.verify(myTransport).send(correctMsg);

how to mock javax.mail.Transport - Java Forum, Transport, and the message is a javax.mail.internet.MimeMessage. I need to mock this Transport so it doesnt send the message anymore. mail.transport.protocol.address-type. String. Specifies the default message transport protocol for the specified address type. The Sessionmethod getTransport(Address)returns aTransport object that implements this protocol when the address is of thespecified type (e.g., "rfc822" for standard internet addresses).

Java Code Examples javax.mail.Transport, This page provides Java code examples for javax.mail.Transport. <p>Can be overridden in subclasses, e.g. to return a mock Transport object. * @see  1 Mocking javax.mail.Transport Jan 28 '19 0 Accessing and modifying ArrayList/TreeSet in a Class from multiple Classes Feb 7 '19 0 Clarity regarding performing unit test in Spring Boot Application Feb 7 '19

Java Code Examples javax.mail.Transport.send, This page provides Java code examples for javax.mail.Transport.send. The examples are extracted from open source Java projects. mail.transport.protocol.address-type. String. Specifies the default message transport protocol for the specified address type. The Sessionmethod getTransport(Address)returns aTransport object that implements this protocol when the address is of thespecified type (e.g., "rfc822" for standard internet addresses).

org.jboss.seam.mock Class MockTransport, org.jboss.seam.mock. Class MockTransport. java.lang.Object extended by javax. mail.Service extended by javax.mail.Transport extended by  The JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications. The JavaMail API is available as an optional package for use with the Java SE platform and is also included in the Java EE platform.

Comments
  • The main advantage of using GreenMail is that your test can then verify that the expected email was sent. I've added in my tests things like this: MimeMessage[] messages = mailServer.getReceivedMessages(); assertEquals(1, messages.length, "Failed to receive registration email"); assertEquals(1, messages[0].getHeader("To").length, "Malformed To headers in received registration email"); assertEquals(user.getEmail(), messages[0].getHeader("To")[0]);
  • unfortunately this is only on the test, but if "Transport" is used inside the implementation class, then it won't be testable.
  • Inject your mock into the class that uses it, before testing that class-- which is actually the most common way to use mocks.