• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to Learn Unit Testing and Need Help

 
Jason Ferguson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34973
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jason Ferguson
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34973
378
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic