1

I'm developing a Spring Boot Web API, and I'm currently writing the required units tests.

I was wondering : Isn't writing the units tests (JUnit + Mockito) for the controllers sufficient ? Since the controllers are the entrypoint of my application and that all the logic that is implemented within the Service side is called from the exposed API, why do I need to write test for the Service side ?

3
  • 2
    You should write JUnit test cases for your service as they ensure your service logic is working as expected. Also, if a person views your code a few years later and if he/she tries to add some code to an existing functionality, the service tests will fail and he/she will know that their change is breaking part of the business logic. They will have to change the tests accordingly to assure everything is working fine. Commented Apr 24, 2020 at 16:58
  • @nrai You suggest that I write Unit tests only for the service layer, not for the Controllers ? Commented Apr 24, 2020 at 17:05
  • 2
    I am suggesting you should write JUnit tests for both controllers and services in your application. It's necessary to write for Controllers as well since they are like an entry point, it would make sense to ensure via tests that your endpoints are exactly what you've defined in the Controller. Nobody can tamper/break the code if you have these tests in place. Commented Apr 24, 2020 at 17:09

2 Answers 2

2

First of all, if you write your tests to cover "required level of tests" or requirement to "have some tests at all" having the production implementation already done, it is slightly too late. In the majority of cases having tests first, based on your requirements, contract, use case or anything it more optimal approach. Nevertheless, I don't know your situation and the thing you're trying to implement, so treat it as a suggestion and move on to the key thing you are asking about.

Your JUnit (preferably 5) and Mockito tests, which probably use MockMvc are very good unit(-like) tests to cover web communication concerns such as: HTTP request types, content type, encoding, input and output parameters JSON (de)serialization, error handling, etc. They are best to test with the service layer mocked. Thanks to that you can easily cover a lot of web cases without the need to prepare data in a database, etc.

The core logic has to also be tested. Depending what it is, it might be feasible to test it in the unit way (the easiest to write, can cover a lot of - also corner - cases). It could be supplemented with some set of integration tests to verify that it works fine also in integration (Spring Beans, DB, etc.).

If desired, you may also write some E2E test from the web call via (real) HTTP requests through controllers, services to a database/datastore (if any), but I would limit it only to the most important scenarios to use it in your CI/CD pipeline to verify that a deployment finished successfully.

Disclaimer. I've found this approach useful in multiple situations, but definitely in some other circumstances it might be good to change the balance point to better apply testing.

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

Comments

2

I think you are probably getting confused between unit and Integration tests.

If you are using Mockito you are probably referring to unit tests wherein, the scope of the Test Class should be only the current class.

Any external method calls should be mocked.So in your case the service calls should be mocked in case you are writing unit test for your controller class.

Your Test Suite should contain

  • Junit for Controller-- To Test the interface contract points like HTTP Method, Request Parameters, Mandatory inputs, Valid Request Payloads.
  • Junit for all other classes including Service classes- This is to test your application classes core logic
  • Integration Test- Finally an integration test which can hit your controller endpoints with service classes and rest of the application code functionality.

Comments

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.