Java Mailing List Archive

http://www.gg3721.com/

Home » user.jmock »

[jmock-user] no difference if testing for true or false? why?

Alexander Lvov

2008-05-16

Replies: Find Java Web Hosting

Author LoginPost Reply
Hi!
Just like many other people writing to this list i am new to unittest and jmock and i cant figure out why
that new jmock test i wrote runs good weather i test it against fail or true (weather its will(returnValue(true)) or will(returnValue(false))).
Well at least one should have failed but both run just fine.
 
The following is my jmock test i am working on:
------------------------------------------------------------------------------------------------
import org.jmock.Expectations;
import javax.mail.*;
import org.jmock.lib.legacy.*;
public class MailSenderBeanTest extends TestCase {

private Mockery context = new Mockery() {
    {
        setImposteriser(ClassImposteriser.INSTANCE);
    }
};

    public void testOneSubscriberReceivesAMessage() throws javax.mail.MessagingException {
     final MailSenderBean msb = context.mock(saab.ecm.bo.MailSenderBean.class);
       
        // expectations
        context.checking(new Expectations() {{
            allowing (msb).sendMail("alexanderl@myhost.se", "alexanderl@myhost.se",
        "subject", "this Text", "dumont.myhost.se"); will(returnValue(true));  
        }});
        // execute
        msb.sendMail("alexanderl@myhost.se", "alexanderl@myhost.se",
    "subject", "this Text", "test.myhost.se");
       
        // verify
        context.assertIsSatisfied();
    }
--------------------------------------------------------------------------------------------------
 
 
 
And i have a following method in my MailSenderBean that i am writing jmock test for
 --------------------------------------------------------------------------------------------------
 public boolean sendMail(String from, String to,
      String subject, String text, String smtpHost){
     
     boolean sent = true;
     boolean debug = false; // true to get more information
//   set the host
     Properties props = new Properties();
     props.put("mail.smtp.host", smtpHost);

     Session session = Session.getInstance(props, null);
     session.setDebug(debug);
     try
     {

      Message msg = new MimeMessage(session);
      InternetAddress iafrom = new InternetAddress(from);
      msg.setFrom(iafrom);
      InternetAddress[] addressTo = InternetAddress.parse(to);
      msg.setRecipients(Message.RecipientType.TO, addressTo);
      msg.setSubject(subject);
      msg.setSentDate(new java.util.Date());
      Multipart multipart = new MimeMultipart();

      MimeBodyPart messageBodyPart = new MimeBodyPart();
      messageBodyPart.setText(text);
      multipart.addBodyPart(messageBodyPart);
      msg.setContent(multipart);
      InternetAddress replyTo[] = new InternetAddress[1];
      replyTo[0] = iafrom;
      msg.setReplyTo(replyTo);
      this.getTransport(msg);
     } catch(MessagingException e) {
      LOG.error("sending status:Failed ", e);
      sent = false;
  }
     
     return sent;
    }
------------------------------------------------------------------------------
 
Please help,
Alexander
©2008 gg3721.com - Jax Systems, LLC, U.S.A.