nar-007
nar-007

Reputation: 169

Spring boot unit testing suggestion.?

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.

Upvotes: 2

Views: 73

Answers (2)

SSK
SSK

Reputation: 3766

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.

Upvotes: 0

Nir Levy
Nir Levy

Reputation: 12953

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

Upvotes: 6

Related Questions