Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

New JavaRanch Journal article: "What It Means to Mock: Isolating Units for Testing"

 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The latest issue of the JavaRanch Journal contains an article by Carrie Prebble on What It Means to Mock: Isolating Units for Testing. Go read it while it's fresh, and discuss it in this thread.
 
Juhan Voolaid
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very good testing framework for mocking and other stuff is Unitils. It supports Spring framework.
 
Russell Bateman
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great article--wish I were advanced enough to understand it better and take advantage of it: Someday, I'm going to leave my world of C behind and get a job writing in God's own language!
 
Piyush Porwal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree, an interesting article with a simple and easy to understand examples. I've few questions though and I tried to answer them myself. Correct me wherever I am wrong.
Questions revolve around the good practices on the getter method of second example (Calendar object):

Q1. Is it a good practice to create and return a new object if it is not set?
Ans: Depends on your requirements, if you want to return null or want to throw an error, you can do that. Author is considering the case, where call to get() should not return a null.

Q2. If object is not set, then why after creating a new object it is not stored to avoid the work of creating a new object one every time?
Ans: Author wants to demonstrate the scenario when every call to get gives back a calendar with todays date i.e. equivalent of calling a new GregorianCalendar();

Q3. Is it a good practice to not to set an object inside get()?
Ans: Again depends on your requirements, but a practice you should not.

Q4. Why cloned object is sent back, if Calendar is not null?
Ans: To isolate the operations on set object and returned (after cloning) object. OR it is again a good practice to return a cloned object (anywhere related to mock ready)?

Pardon me if these questions look silly, or if this is not the right place to ask them.

-Piyush
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
Omar Palomino Sandoval
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a very interesting article that promotes delegation of object life-cycle management. Right now, in my project we are implementing some agile methodologies that includes TDD. [It's an spring + hibernate app]

I think mock objects for testing isolated layers would be useful, the only tradeoff that I see is the use of MockObjectTestCase class through inheritance.

I think we will follow a more decoupled approach, fortunately in the jmock's geeting started site, there are plenty of examples to accomplish this by favoring composition [desing principle: favor composition over inheritance] or even by using annotations.
[ April 14, 2008: Message edited by: Omar Palomino Sandoval ]
 
Carrie Prebble
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piyush P,

The whole reason-for-being for this calendar example is to allow a test to inject a date for the unit to work on. Another solution to writing testable code in the unit might be to have all the methods in the unit accept a Calendar as an argument and return a Calendar.

The Calendar getter returns a clone if the member is not set. If it's not cloned then I couldn't have multiple calendars like:
Calendar startDate = getCalendar();
Calendar endDate = get Calendar();
because they would be the same date and changing one would change the other.

Carrie
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Omar Palomino Sandoval:
I think mock objects for testing isolated layers would be useful, the only tradeoff that I see is the use of MockObjectTestCase class through inheritance.

Note that this isn't a downside of mock objects in general but a downside of the specific library you're using (JMock).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic