• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

testing with mock objects

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

I'm new to testing with mock objects and have an easy and maybe stupid question.

I have a handler class that among other things stores Groups in the database. The groups (and other data) are given in a data object.

For that handler I want write an unit test to check if groups are stored correct.


I found out that's good to use mock objects intead of using the database. So I decide to use EasyMock.

My problem is - in every code I found on the net EasyMock is used as follows:



So I have the problem that I don't provided that in my class. The StorageHandler's daos cannot set from the outside.

My question: Have I rewrite my class to use mock objects to set daos in that way? Or did I misunderstood the princip of using EasyMock?

Many thank for all help !
[ June 13, 2008: Message edited by: Oliver Brocker ]
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oliver,
Welcome to JavaRanch!

This is an English website? Can you post your question in English? While we do have a moderator who speaks German (that's what I think your post is written in, but I'm not sure), it wouldn't allow others to follow the conversation.
 
Oliver Brocker
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm really sorry for wrote my first post in English! I worked from morning to night and I guess I was too tiered ...

For a clean view I now updated my first post.
[ June 13, 2008: Message edited by: Oliver Brocker ]
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oliver,
Yes. You often do need to make changes to a class to make it testable. The nice thing is after a while, that comes naturally and the code gets written in a testable way in the first place.

Common patterns are:
1) Initialize the variable in the constructor (to the value the "real" code will use) and provide a setter to inject the test value.
2) Provide a constructor taking the class to inject and optionally a default constructor which passes the value the real code will use to the new constructor.
3) Make the field to inject a package private field and set it directly.

I like options #1 and #2 much better, but have seen all three.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed the thread subject to English so people don't think they won't understand the thread.
 
Oliver Brocker
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your help! I have rewrite my classes now to try your first suggestion.

Do you know a good tutorial, ebook, book or code examples about testing daos with mock objects? I still have great difficulties by using JMock or Easymock and don't know where I can get more information.

So all examples I find on the net uses the old version of jMock (1.2.0). But for jMock 1.2.0 only a very simple documentation is availbale.
The documentation for jMock 2.40 is better but I didn't find any code example.

Typical problems of a beginner .
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oliver,
Thanks for updating the original post.

Book - "Test Driven" has a whole chapter on testing daos. It's an excellent book. The author happens to be a moderator here.

Which leads me to an article - this same author wrote an article on this same topic for our JavaRanch Journal. I'd start with that. If you like the article, the book is a good investment. It covers a lot of things that you will need to know about testing as you progress.
 
Oliver Brocker
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks for the link and the book advice .
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic