2

Wondering if it is possible to get rid completely of SQL dependency. Let's say I'm writing test which communicates with DB, this introduces the need to manage DB schema, this is very cumbersome as in-memory DB often do not match to production type DBs.

Would it be possible to test with Hibernate/JPA and no underline DB Schema?

I know so far of two options, I'd like here to broaden my knowledge, if there are any other possibilities, please share.

  1. mock the hibernate
  2. use fake JDBC driver

Or should I just focus on mocking DAO layer and not wasting time with this at all? Sanity checking JPA entities vs DB is completely different story.

7
  • This makes no sense. Tests should show problems and you like to mock something that has problems to ignore them? Commented Nov 4, 2015 at 15:02
  • But what if you are in your exact unit test focused in some other fail spots. I mean you are not testing the DB you need to test some functionality and you need to have some easy way how to cut out the dependency on DB. You need in this case to mock the DB behavior in way you expect it to work under normal conditions. Commented Nov 4, 2015 at 15:16
  • To cut-out the DB is a normal condition? Why test the code and Mock some if you can test all normal conditinos in integrationtests? Commented Nov 4, 2015 at 16:16
  • I guess it is purpose of unit testing. Integration testing is not really some lightweight undergoing. In integration test you should in my opinion check just the integration with other systems(included DB). For units it is common practice to skip the complexity of external systems. Commented Nov 5, 2015 at 5:46
  • 1
    Depends on how you use hibernate. Including n-m/1-n relations! Can you Climb the Records from LOGIN to USER to ADDRESS to ORDERS to DELIVERYS to get the status of the last order? They are deep nested mock what you shall avoid! Commented Nov 5, 2015 at 13:11

1 Answer 1

2

You fear the difference between an in-memory database and the production database? But you don't fear the much greater difference between your two "mock" options and production?

From my point of view any replacement of the production database for testing will create false positives somehow. And I would guess that an in-memory database will mimic the real environment best. Especially if you use something like H2 which tries to support different SQL dialects.

As long as you only write small and encapsulated unit tests a mock of your DAO layer may help as well. But as soon as you are writing integration tests you will need to provide a database somehow.

And no - Hibernate can't work without a database schema or connection.

Sign up to request clarification or add additional context in comments.

5 Comments

The question is more fore unit testing, anyway just now I'm investigating little bit closer to integration testing. You are right about the big difference between mock and DB. The integration test should be the last automated point before shipping the code out. My concerns are now how much are the embedded DB reliable(mimicking the production DB) There for sure is always some risk involved(not to mention that some case are unable to mimic at all). The thing is how close should we be to the production? For me the closest we can get, in automated way is to use containers(Docker) with real DBs
And even if you mimic the production database to the full extent - you will need to write tests that detect the errors that are production specific and stable - no one likes to change tests again and again just because the data in production has changed. And even those tests wouldn't find all errors. I think you will end up with integration tests that work with a (somehow stable) in-memory database. That is what I've got in many of my projects.
Just out of curiosity, how are you exactly doing integration testing. Setup env, checking the assertion etc.
First of all I'm using my own tool Fastnate for creating the testdata. And with cargo I deploy and start a test server that runs against H2 (either with auto schema creation or Flyway). The tests are usually Selenium tests with page objects and an own framework on top (not open source up to now) that makes it easy to write those page objects and tests.
One of the reasons why I started this question, was idea what if I will use hibernate to generate the SQL, what use it will have and here we go Fastnate. Will definitively check it out. I have used Cargo for remote deploy, but what I am missing is the automated env.setup. This is why Arquilian and Docker are in my focus now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.