Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
raminaa niilian
Ranch Hand
Posts: 551
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thank you for reading my post.
Is there any way to unit test JDBC code without connecting to database?
Thanks
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use HSQLDB with an in-memory database; that way, not even a file is created, which is handy for testing.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18025
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18025
47
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic