aspose file tools*
The moose likes Testing and the fly likes is there any way to unit test JDBC code without connecting to databas? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "is there any way to unit test JDBC code without connecting to databas?" Watch "is there any way to unit test JDBC code without connecting to databas?" New topic
Author

is there any way to unit test JDBC code without connecting to databas?

raminaa niilian
Ranch Hand

Joined: Jul 14, 2005
Posts: 551
Hi
Thank you for reading my post.
Is there any way to unit test JDBC code without connecting to database?
Thanks
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42595
    
  65
You can use HSQLDB with an in-memory database; that way, not even a file is created, which is handy for testing.


Ping & DNS - my free Android networking tools app
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16228
    
  21

I'm using JUnit attaching to an in-memory instance of Derby.

The production system uses DB2, and there are a few cases where the difference between DB2 and Derby cannot be unit-tested, but overall it works well.

JUnit can also roll back changes after a test so even if you use a permanent database for testing, the tests can be made to run from a consistent baseline instead of continuously polluting the test database.


Customer surveys are for companies who didn't pay proper attention to begin with.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

This may just be semantics, but I wouldn't consider any test that needs a database (in memory or on disk) a unit test. I'd call it an integration test.


GenRocket - Experts at Building Test Data
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16228
    
  21

Well, the database unit testing that I do is testing individual DAOs at the lower level and individual business services at the higher level, so I'd consider them as units.

The fact that they need to invoke (or mock) external services is sort of beside the point, since I'm not testing the services themselves, which are tested as units at lower levels.

I'm VERY keen on DAO unit-testing, though, since SQL is an interpreted language and thus isn't validated at compile-time, but I don't want to set up a complex framework for testing the database layers, since not only is it more work, it has a higher risk of having unrelated problems obscure or interfering with what I'm actually attempting to test. So I use the database to serve as the validator.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Ah, you said the key work there: Mock.

I get what you're saying though. Like I said, semantics. I'll back track on my in-memory being an integration test and concede that unit testing is still appropriate given that you're asserting SQL.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: is there any way to unit test JDBC code without connecting to databas?