Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Mocking and Stubbing in Java - (JUnit, etc)

forumKid2May 5 2010 — edited May 5 2010
Currently our junit tests all eventualy read the database to fill the objects and test code.

My question is, isnt it a better practice to mock/stub and avoid the trip to the database, since we are testing the logic, not the database connection?

But if my thought is right on mocking and stubbing, how can it be made possible?

For example. Lets say I have class foo which calls dataHandler.java which in turn called dataAccessor.java. There is no way that I know of that inside the foo class we could tell the dataHandler class to override the dataAccessor class and instead of using that dataAccessor which calls the database, use another class such as dataAccessorForUnitTesting and inside that class simply fill the requested object with dummy data and return it. Basically saying foo cannot override a class thats multilpe levels deep.

Just looking for some suggestions on people who use testing such as junit. If the right way is to read the database, then so be it. I'd just like someone to point that out from their experience.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 2 2010
Added on May 5 2010
6 comments
791 views