Reputation: 3336
I have a application with 3 layers: Controller, Service and Repository.
My controller is a Rest (@RestController in spring) that only get a request using a DTO, convert to a Model and pass to a service (@Service in spring).
My service has all business logic and my test classes are based on service classes only.
My repository is only a JpaRepository and sometimes use a custom query with @Query.
Well, as I said, I just make test classes for Service, because here is my business logic. My question is about how to create test for controller classes, is it really necessary ? Is this a good practice ?
Upvotes: 0
Views: 519
Reputation: 24452
Of course it’s a must. We have to test all layers of the applications, as each or them provides particular elements: service-business logic, repository-data access, controller-http input and output.
For each endpoint, you can test if it returns the appropriate status code, headers, body, etc. Furthermore, you should test error responses, as well. Think of all the possibilities per endpoint, 200, 201, 4xx, 5xx cases.
Upvotes: 3