mardi 30 octobre 2018

Testing DAO and Service layer with in-memory db and mocking

During my learning of testing in Java, I faced a thing which I didn't really get. So, basically, I have unit tests for DAO where I test CRUD operations with the help of in-memory database HSQL.
Well, the next step is to test the Service layer. On the web, they say to use mocks to mock DAO and then test Service.

I see only one way to use mocks: if there are some methods in Service and those methods don't exist in DAO. Then, we don't need to test CRUD operation(DAO itself), therefore we mock DAO and test only Service.

  1. But what If I have the same methods in Service as in DAO?
  2. And what If I just use an in-memory database to test Service as I did with DAO?

I'd like to know what are the benefits to use mocks instead of an in-memory database. Moreover, I guess, it is less readable when use Mocks.

If it makes any sense - I use Spring.

Aucun commentaire:

Enregistrer un commentaire