Turnleaf Design Ramblings of a junior developer

10Nov/090

Mocking JDBC Connections with MockRunner

Last week I published an article, An Intro into Test Driven Development with Junit4, which got somewhat mixed reviews. One particular commenter on reddit suggested I didn't understand that unit tests shouldn't talk to its data source. I decided to do some investigating into his claims, you can check out my findings here. I was however intrigued none the less and started looking for a tool that would allow me to easily mock a JDBC connection. I found Mockrunner to be the easiest to use of the ones I have found. Below is a brief tutorial on how to use Mockrunner. (Keep in mind that I am still fairly new to using Mockrunner)

In the class I am testing I am explicitly calling the columns I am retrieving by name. So I need to add these column names to my mock ResultSet.

Obviously I also retrieve data from a result set so I must also insert data into the mock ResultSet. There are several ways to do this, but the easiest I found was using a list. You must add elements to the list in the same order as you added columns to the ResultSet.

In my actual test case I can verify that I am actually gathering the contents of the result set correctly and that the correct SQL statement was run.

This is but scratching the surface of MockRunner's potential and using mocks at large. I hope this brief tutorial was helpful in getting you started using MockRunner. You can checkout the entire test class here and the class that I am testing here. If you want to get an entire working version of the project be sure to read the technical guide. Let me know if you have any questions or want to add your own comments.

Bookmark and Share

Technorati Tags: , , ,

Related posts:

  1. Ramblings: Should unit tests talk to a data source?
  2. A brief intro into unit testing with JUnit
  3. An Intro into Test Driven Development with JUnit4
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.