GeeCON Prague 2014*
The moose likes Testing and the fly likes A few questions about Mock Objects and JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » Testing
Bookmark "A few questions about Mock Objects and JDBC" Watch "A few questions about Mock Objects and JDBC" New topic
Author

A few questions about Mock Objects and JDBC

Marshall Levin
Greenhorn

Joined: Nov 29, 2004
Posts: 4
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

Joined: May 26, 2003
Posts: 30580
    
154

"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


[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
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30580
    
154

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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
wood burning stoves
 
subject: A few questions about Mock Objects and JDBC