Spring boot unit testing suggestion.?

Viewed 50

I have a controller class and a service class. Controller has endpoints and service layer has functions performing something specific (inserting, loading a file and counting lines, updating, etc).

Now what I want to know is, should I unit test an endpoint(controllers) as a whole or individual functions present in the service layer? I've really been confused. Please help.

2 Answers

Yes, you should have two test classes-

One for the controller, with the service mocked, testing the controller's functionality.
One for the service, with the dal layer mocked, teating the service

You need two test classes

  1. controller class testing
  2. service class testing

Controller class test - This class will test the your endpoints and their functionality. You can achieve this using RequestBuilder and MockMvc. You can mock your service class call.

Service class test - This class will test your actual business logic (inserting, loading a file and counting lines, updating, etc.). You can mock your repository call and any other calls if there.

Related