vb.net 2d barcode dll A Mock MailSender Implementation in Java

Creator Data Matrix ECC200 in Java A Mock MailSender Implementation

Listing 10-18. A Mock MailSender Implementation
Drawing ECC200 In Java
Using Barcode encoder for Java Control to generate, create ECC200 image in Java applications.
www.OnBarcode.com
Data Matrix ECC200 Decoder In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
package com.apress.timesheets.mail; import import import import import import java.io.InputStream; java.util.*; javax.mail.*; javax.mail.internet.MimeMessage; org.springframework.mail.*; org.springframework.mail.javamail.*;
QR-Code Creation In Java
Using Barcode generator for Java Control to generate, create QR image in Java applications.
www.OnBarcode.com
Print GTIN - 128 In Java
Using Barcode generator for Java Control to generate, create USS-128 image in Java applications.
www.OnBarcode.com
public class MockJavaMailSender implements JavaMailSender { private Session session; private List<Message> mimeMessages = new ArrayList<Message>(); private List<SimpleMailMessage> simpleMessages = new ArrayList<SimpleMailMessage>(); public MimeMessage createMimeMessage() { try { final Provider provider = new Provider(Provider.Type.TRANSPORT,"smtp",
Barcode Printer In Java
Using Barcode creator for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
ECC200 Creator In Java
Using Barcode drawer for Java Control to generate, create Data Matrix ECC200 image in Java applications.
www.OnBarcode.com
CHAPTER 10 TESTIN G
QR-Code Creation In Java
Using Barcode drawer for Java Control to generate, create QR-Code image in Java applications.
www.OnBarcode.com
Creating Postnet 3 Of 5 In Java
Using Barcode creator for Java Control to generate, create Postnet image in Java applications.
www.OnBarcode.com
MockTransport.class.getName(),"Apress","1.0"); session = Session.getInstance(new Properties()); session.setProvider(provider); return new MimeMessage(session); } catch( final NoSuchProviderException e) { throw new RuntimeException("Mock JavaMailSender provider failed",e); } } public MimeMessage createMimeMessage(final InputStream inputStream) throws MailException { throw new UnsupportedOperationException( "Mock object does not support this method"); } public void send(final MimeMessage message) throws MailException { mimeMessages.add(message); } public void send(final MimeMessage[] messages) throws MailException { for( final Message message : messages ) mimeMessages.add(message); } public void send(final MimeMessagePreparator preparator) throws MailException { try { final MimeMessage message = createMimeMessage(); preparator.prepare(message); mimeMessages.add(message); } catch(Exception e ) { throw new MockMailException(e); } } public void send(final MimeMessagePreparator[] preparators) throws MailException {
Printing Data Matrix In Java
Using Barcode drawer for Android Control to generate, create DataMatrix image in Android applications.
www.OnBarcode.com
ECC200 Drawer In None
Using Barcode generator for Office Excel Control to generate, create Data Matrix ECC200 image in Office Excel applications.
www.OnBarcode.com
CHAPTER 10 TESTING
Code39 Creator In .NET
Using Barcode printer for Reporting Service Control to generate, create Code 39 Extended image in Reporting Service applications.
www.OnBarcode.com
USS Code 39 Encoder In Java
Using Barcode generator for BIRT Control to generate, create Code-39 image in Eclipse BIRT applications.
www.OnBarcode.com
try { for( final MimeMessagePreparator preparator : preparators ) { final MimeMessage message = createMimeMessage(); preparator.prepare(message); mimeMessages.add(message); } } catch(Exception e ) { throw new MockMailException(e); } } public void send(final SimpleMailMessage message) throws MailException { simpleMessages.add(message); } public void send(final SimpleMailMessage[] messages) throws MailException { for( final SimpleMailMessage message : messages ) { simpleMessages.add(message); } } public List<Message> getMimeMessages() { return mimeMessages; } public List<SimpleMailMessage> getSimpleMessages() { return simpleMessages; } public int getMessageCount() { return mimeMessages.size()+ simpleMessages.size(); } public void clear() { mimeMessages.clear(); simpleMessages.clear(); } }
Code39 Creation In VS .NET
Using Barcode generator for ASP.NET Control to generate, create Code 39 image in ASP.NET applications.
www.OnBarcode.com
EAN / UCC - 13 Generator In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create EAN / UCC - 14 image in ASP.NET applications.
www.OnBarcode.com
CHAPTER 10 TESTIN G
Paint GTIN - 13 In None
Using Barcode generator for Online Control to generate, create UPC - 13 image in Online applications.
www.OnBarcode.com
Barcode Generation In Java
Using Barcode maker for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
Because the JavaMail API uses a static factory method to associate MIME message objects with the underlying e-mail transport (usually SMTP), we need to register a custom transport with JavaMail in the createMimeMessage() method. We omit the implementation of the version of createMimeMessage(InputStream) method entirely because it is not used by our implementation. But an appropriate exception is generated so that if refactoring causes the method to become part of the code path of a class under test, we will not flounder around trying to determine the cause of mysterious failures! The rest of the methods take the populated MimeMessage or SimpleMailMessage instances for transmission and instead store them in lists for later retrieval by the unit test. Additional utility methods are provided to access these lists, to obtain the count of messages contained within them, and to clear them of their contents upon test completion. Listing 10-19 shows the environment that our three mail DAO test methods will operate within. We establish standard parameters for the sender, recipient, and subject of the e-mails that will be applied to the e-mails to be transmitted. We establish the mock instance of the JavaMailSender to be supplied to the DAO implementations (shown in bold in Listing 10-19), and we create a suitable timesheet object that will be passed to the MailDao s sendTimesheetUpdate method for each case. We also provide a tearDown() method that will clear the mock JavaMailSender s message stores after each test has completed.
Data Matrix ECC200 Drawer In .NET Framework
Using Barcode creator for ASP.NET Control to generate, create DataMatrix image in ASP.NET applications.
www.OnBarcode.com
Make Quick Response Code In C#
Using Barcode generation for .NET framework Control to generate, create QR Code image in VS .NET applications.
www.OnBarcode.com
Listing 10-19. The Environment of the Unit Test
1D Drawer In VS .NET
Using Barcode creator for ASP.NET Control to generate, create 1D image in ASP.NET applications.
www.OnBarcode.com
Encoding Barcode In Objective-C
Using Barcode printer for iPhone Control to generate, create Barcode image in iPhone applications.
www.OnBarcode.com
private static final String FROM = "from@example.com"; private static final String TO = "to@example.com"; private static final String SUBJECT = "subject"; private final MockJavaMailSender mailSender = new MockJavaMailSender(); private Timesheet timesheet; @Override protected void setUp() throws Exception { final UserAccount account = new UserAccount("username"); final Calendar startDate = Calendar.getInstance(); timesheet = new Timesheet(account,startDate); } @Override protected void tearDown() throws Exception { mailSender.clear(); }
CHAPTER 10 TESTING
With the environment defined, we can now examine the simplest of the three tests. Listing 10-20 shows the test of the SimpleMailDaoImpl implementation of the EmailDao interface.
Listing 10-20. The SimpleMailDaoImpl DAO Implementation
public void testSimpleMailDao() throws MessagingException,IOException { final SimpleMailDaoImpl impl = new SimpleMailDaoImpl(); impl.setFromAddress(FROM); impl.setRcptAddress(TO); impl.setSubject(SUBJECT); impl.setMailSender(mailSender); impl.sendTimesheetUpdate(timesheet); assertEquals(1,mailSender.getMessageCount()); final List<SimpleMailMessage> received = mailSender.getSimpleMessages(); assertEquals(1,received.size()); final SimpleMailMessage message = received.get(0); assertEquals(1,message.getTo().length); assertEquals(TO,message.getTo()[0]); assertEquals(FROM,message.getFrom()); assertEquals(SUBJECT,message.getSubject()); assertEquals("A timesheet has been updated by user: username", message.getText()); } The first part of the test implementation creates and populates the SimpleMailDaoImpl implementation to be tested. The second part calls the sendTimesheetUpdate method, the behavior to be tested. The third part retrieves the sent message from the mock mail sender, and checks to see whether it contains the correct information. Our assumption is that the real MailSender implementation (provided as a part of the Spring framework) is reliable and trustworthy, so that if the message is correct when it is passed to the mail sender, the message will be transmitted correctly. To be sure, thirdparty implementations are sometimes incorrect but if we have doubts, we can add tests of the third-party implementations to verify their behavior. Listing 10-21 shows the test corresponding to the VelocityMailDaoImpl implementation of the EmailDao interface.
Copyright © OnBarcode.com . All rights reserved.