This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A few questions about Mock Objects and JDBC

 
Marshall Levin
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I'm quite new to Mock Objects. I'm using the 0.09 download from mockobjects.com.

First, it seems that these mock objects don't seem to like CallableStatement. Most, if not all, of the methods return "not implemented." I was able to get around this by using a prepared statement instead, which worked OK in one case (just an update where nothing was going to be returned). But in another place, I need to use a stored procedure with an out parameter -- I'd rather not use a result set of one row with one value -- so I'd like to be able to use mock objects on a CallableStatement.

Second, it just seems unnatural to me to have to pass a Connection object to the method(s) I want to test, and it seems unnatural to me to have to make these methods public in order to test them. Am I doing something wrong?

Finally, following some examples in http://www.mockobjects.com/DevelopingJdbcApplicationsTestFirst.html, I was able to test some JDBC code in one of my methods. But now I need to add another section of JDBC code within that same method I'm testing -- the program needs to query something, do some work and then perform an update. How can I mock TWO independent JDBC calls within the same method?

My test code sets up the expected prepared statement SQL, the expected parameters to be set, etc., then calls the method to be tested, then calls verify() to make sure everything was done correctly. But now if I have two JDBC sections in that method, I can't figure out how to test them.

Thanks in advance for your help!

- ML
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33696
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"ML",
We're pleased to have you here with us in the Testing forum, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks,
Jeanne
Forum Bartender
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 33696
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you haven't invested much time in Mock Objects, I would suggest using a dynamic framework like jMock or easyMock. These emphasize programming to interfaces rather than providing actual mock implementations. You record the calls and responses. They also have the advantage of being more flexible with calling multiple times.

If you want to use Mock Objects, you can always extend the CallableStatement object with your own implementation. I had to do that for some of their servlet mocks before I switched to easyMock.

You don't have to pass the Connection to the method. You can pass it to the object in the constructor or set it through a setter. The idea is to decouple the Connection object from the class. This needs to be done using any framework and is good design anyway.

You don't have to make the method public for testing. It is common to make them package-private (no modifier) and put the tests in the same package (or better, a parallel directory with the same package name.) There is much debate in this and the OO forum about whether the methods should be public anyway. I personally think the method should be package-private the it would have been private were there no tests.

Second, it just seems unnatural to me to have to pass a Connection object to the method(s) I want to test, and it seems unnatural to me to have to make these methods public in order to test them. Am I doing something wrong?

Finally, following some examples in http://www.mockobjects.com/DevelopingJdbcApplicationsTestFirst.html, I was able to test some JDBC code in one of my methods. But now I need to add another section of JDBC code within that same method I'm testing -- the program needs to query something, do some work and then perform an update. How can I mock TWO independent JDBC calls within the same method?

This sounds like it should be two or more methods. If you want to test on a higher level, you need to write your own mock object or use one of the dynamic frameworks I mentioned above.

And welcome to JavaRanch!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ML:
I'm using the 0.09 download from mockobjects.com.

First, it seems that these mock objects don't seem to like CallableStatement. Most, if not all, of the methods return "not implemented."


Well, notice that this is a version lower than one, which basically means that not everything works as expected yet.

Also notice that it's open source, so you are welcome to download the source code, implement the methods you need and provide a patch to the project team, so that they can include your changes in the next version.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic