This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Testing and the fly likes testing with mock objects Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Engineering » Testing
Reply Bookmark "testing with mock objects" Watch "testing with mock objects" New topic
Author

testing with mock objects

Oliver Brocker
Greenhorn

Joined: Jun 11, 2008
Posts: 11
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 ]
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26499
    
  78

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.


[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
Oliver Brocker
Greenhorn

Joined: Jun 11, 2008
Posts: 11
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 26499
    
  78

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 26499
    
  78

I changed the thread subject to English so people don't think they won't understand the thread.
Oliver Brocker
Greenhorn

Joined: Jun 11, 2008
Posts: 11
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
internet detective
Marshal

Joined: May 26, 2003
Posts: 26499
    
  78

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

Joined: Jun 11, 2008
Posts: 11
Many thanks for the link and the book advice .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: testing with mock objects
 
Similar Threads
optiontransferselect example
Unable to fully utilize EasyMock. Not compliance with Test Driven Approach.
planning test data and doing negative testing with junit
Unit testing struts
Data Access Objects: static methods