The following test is taking around 5 seconds to execute due to the inclusion of m.saveChanges()
.
import org.junit.Before;
import org.junit.Test
Fix the most common mistakes people make when using JavaMail in your code first.
DNS lookup can hurt performance on some machines. For the JDK you can change the security properties for caching DNS lookup networkaddress.cache.ttl and networkaddress.cache.negative.ttl or set the system properties sun.net.inetaddr.ttl and sun.net.inetaddr.negative.ttl. The default behavior in JDK 7 and later does a good job of caching so you shouldn't have to change these settings.
Preferably, you can use the session properties to avoid some these lookups.
false
if your code is relying on the setFrom() but this will be handled if you applied point #1.false
which is the default value.Since your are not transporting a message, apply rule #1 by changing your code to:
@Test
public void test1() throws MessagingException, IOException {
Properties props = new Properties();
props.put("mail.host", "localhost"); //Or use IP.
Session s = Session.getInstance(props);
MimeMessage m = new MimeMessage(s);
m.setContent("<b>Hello</b>", "text/html; charset=utf-8");
m.saveChanges();
assertEquals(m.getContent(), "<b>Hello</b>");
assertEquals(m.getContentType(), "text/html; charset=utf-8");
}
If you are transporting a message then combine the rules #1, #2, and #3 which will prevent accessing the host system for a name lookup. If you want to prevent all DNS lookups during a transport then you have to use IP addresses.