File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Testing and the fly likes Trying to Learn Unit Testing and Need Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "Trying to Learn Unit Testing and Need Help" Watch "Trying to Learn Unit Testing and Need Help" New topic
Author

Trying to Learn Unit Testing and Need Help

Jason Ferguson
Ranch Hand

Joined: Aug 09, 2007
Posts: 58
I'm trying to learn as I go for unit testing, but need a unit test for a method that uses Spring's JavaMailSenderImpl class.

Here's the info:

applicationContext.xml bean for Mail Sender:



Actual method to test (note: mail sender is already injected by Spring):



I guess I need to somehow mock the mail sender to avoid sending out multiple messages. However, I'm just getting started and do not know how to proceed.

Help would be greatly appreciated.

Jason
[ March 04, 2008: Message edited by: J. Ferguson ]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30123
    
150

Jason,
Before you worry about the mail sender, can you write a test that calls the DAO and has the DAO return an empty list? This gets you set up with whatever mock objects library you are using with a simpler case since it skips the whole loop.

Once you have that, you have demonstrated mocking out an interface. org.springframework.mail.javamail.JavaMailSender is an interface too. Which means you can try using the same techniques. NOte that while MailSender is injected by Spring when you run "for real", it is not injected during unit testing. It is your unit test's responsibility to inject it.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jason Ferguson
Ranch Hand

Joined: Sep 16, 2007
Posts: 47
Jeanne,

I finally moved past that last night, by using EasyMock to mock JavaMailSender.

The only problem was that I ended up changing the return type of the method to return a rather odd Map that contained an array of all the SimpleMailMessage objects generated by the class and a few other items generated internally by the method. I'm not overly happy with that, but it's testable :/

Jason

Originally posted by Jeanne Boyarsky:
Jason,
Before you worry about the mail sender, can you write a test that calls the DAO and has the DAO return an empty list? This gets you set up with whatever mock objects library you are using with a simpler case since it skips the whole loop.

Once you have that, you have demonstrated mocking out an interface. org.springframework.mail.javamail.JavaMailSender is an interface too. Which means you can try using the same techniques. NOte that while MailSender is injected by Spring when you run "for real", it is not injected during unit testing. It is your unit test's responsibility to inject it.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30123
    
150

Jason,
I'm glad you got past it. For the future, note that requires a clumsy hack in order to be made testable is often crying out to be refactored.

For example, the string building stuff could be in a separate testable method. Or the whole thing could be extracted into an object with state. The state would be equivalent to what you put in the map, but it would be "real."
Jason Ferguson
Ranch Hand

Joined: Sep 16, 2007
Posts: 47
Originally posted by Jeanne Boyarsky:
Jason,
I'm glad you got past it. For the future, note that requires a clumsy hack in order to be made testable is often crying out to be refactored.



Already working on it. I've moved the mail code into a separate (protected) method, and will be moving the code to create the notification message into yet another method.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Trying to Learn Unit Testing and Need Help
 
Similar Threads
Configuring Email Sending with Spring
Error when deploying an EAR on JBoss 5.1.0
Can we attach multiple email addresses through spring email
Non-SOAP HTTP Client for FedEx WebService
mail-service.xml - multiple smtp accounts