2

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
  • 5
    Both. Separately. Commented Mar 13, 2020 at 18:19
  • 2
    Unit testing means isolating and testing units separately. You should have two test cases. If both pass then both should work together. Commented Mar 13, 2020 at 18:27

2 Answers 2

6

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

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

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.