|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
no difference if testing for true or false? why?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@...", "alexanderl@...", "subject", "this Text", "dumont.myhost.se"); will(returnValue(true)); }}); // execute
msb.sendMail("alexanderl@...", "alexanderl@...", "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
|
|
|
Re: no difference if testing for true or false? why?It looks like you're mocking the method you're testing, so the real
thing is never called. S. On 16 May 2008, at 17:13, Alexander Lvov wrote: > 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@...", " > alexanderl@...", > "subject", "this Text", "dumont.myhost.se"); > will(returnValue(true)); > }}); > // execute > msb.sendMail("alexanderl@...", "alexanderl@...", > "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 Steve Freeman Winner of the Agile Alliance Gordon Pask award 2006 http://www.m3p.co.uk M3P Limited. Registered office. 2 Church Street, Burnham, Bucks, SL1 7HZ. Company registered in England & Wales. Number 03689627 --------------------------------------------------------------------- To unsubscribe from this list, please visit: http://xircles.codehaus.org/manage_email |
|
|
RE: no difference if testing for true or false? why?There are others here who can probably give you more detailed
advice, but it looks like you are using the mock incorrectly. If you are
writing a test for the MailSenderBean, then you would not create a mock for
it. You would create an actual version of it. Looking at the method that you want to test, I don’t
actually see anything that you would create a mock for. The mock would be
created for any class that the MailSenderBean interacts with. For
example, lets say you had a “MailProtocol” class that actually did
the sending of the message. The MailSender would have the MailProtocol
injected into it, and the MailSender would do the work of assembling the mail
message and then calling the ‘send’ method on the
MailProtocol. You would mock the MailProtocol and inject the mock into
the MailSender. You would then set up an expectation for the MailProtocol
mock, specifying exactly what parameters should be passed in to the ‘send’
method on it. You would then call the ‘sendMail’ method on
your MailSenderBean with a certain set of parameters and jmock does the work of
making sure that they were assembled and correctly passed to the ‘send’
method of your MailProtocol mock. What you have done below is create a mock object, set up an
expectation that a method will get called on the mock object and then you
called that method yourself on your mock object, guaranteeing that the test
will pass every time while at the same time not actually doing anything with
the code you actually want to test. I hope I haven’t misunderstood anything you are doing
below and given you incorrect advice… Chris From: Alexander Lvov
[mailto:alexanderlvov1@...] 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 org.jmock.lib.legacy.*; public class MailSenderBeanTest extends TestCase {
final MailSenderBean msb =
context.mock(saab.ecm.bo.MailSenderBean.class); // expectations // execute -------------------------------------------------------------------------------------------------- And i have a following method in my MailSenderBean
that i am writing jmock test for -------------------------------------------------------------------------------------------------- public boolean sendMail(String from, String to, // set the host
try InternetAddress[] addressTo =
InternetAddress.parse(to); msg.setSubject(subject);
multipart.addBodyPart(messageBodyPart); this.getTransport(msg); } catch(MessagingException e) { ------------------------------------------------------------------------------ Please help, Alexander No virus
found in this incoming message. |
|
|
Re: no difference if testing for true or false? why?Thank you guys! Yes i realise now it was wrong to use jmock that way. I guess in this case its enough to simply do
a simple junit test.
/Alexander 2008/5/16, Chris O'Connell <oconnell@...>:
|
| Free Forum Powered by Nabble | Forum Help |