lundi 1 août 2016

Unit testing strategies for a layered Java Rest application

I am currently puzzled regarding the right way I should do unit tests. Let's say we have to write JUnit tests for a basic Java Rest CRUD application composed of these "common layers" where each layer calls the sub layer:

  • Controller layer (ex: AccountRestController.getAccount(id) - returns JSON of Account)
  • Service layer: (ex: AccountServive.getAccount(id) - returns Account object )
  • Repository layer (ex: AccountRepository.getAccount(Id) - returns Account object )
  • Domain layer (ex: Account (id, name) )
  • Database table (ex : Account(ID, NAME) )

We would also have the following hypothesis (or restrictions) for the Unit tests (not sure they are appropriate though ?)

  • They have to be out of container (no Tomcat\Jetty and no in memory database - I guess I would do that in my integration test)
  • Use mocking (for example Mockito framework)

So my questions are:

  • What is the best way\the right way\the best practices to write unit tests for this type of application ?
  • To be rigorous, do we have to unit test each layer(controller, service, repository, domain) independently by mocking each time the sub layer
  • Would unit testing only the top Rest Controller be enough?
  • ... and again ... are my hypothesis appropriate ? (Couldn’t we do Unit testing with a container & in memory database?)

Regards

Aucun commentaire:

Enregistrer un commentaire