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.
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, 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.
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
posted
0
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.
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.