1

In our ASP.NET Core application, we have many APIs which are calling public method from the service class. Service class is implementing an Interface. Each service method (depending upon complexity and requirement) may call any number of internal or private or helper methods. While each of these internal/private/or helper methods are separately unit tested, how do we test the public service methods in such a way that we don't have to duplicate the code/effort that we have already spent in testing the internal/private/helper methods?

E.g. we can easily mock up the Interface methods to say the interface method expects these input arguments, and returns bla bla (without actually calling the method). So, can a similar thing be done to internal/private/helper methods which are not part of Interfaces so that we don't have to actually call these methods real time, and can just mock them while testing the main method itself?

lennon310
  • 3,132
  • 6
  • 16
  • 33
WAQ
  • 113
  • 4
  • Maybe don't unit test it, but only run integration tests? If all the single building blocks of your service logic are already under unit tests, there is not much to gain by adding on unit tests that don't test something new. What is of interest, however, is whether all the building blocks fit togehter - that's what integration tests are for. – germi Sep 14 '21 at 13:34

1 Answers1

5

how do we test the public service methods in such a way that we don't have to duplicate the code/effort that we have already spent in testing the internal/private/helper methods?

Don't unit test your internal/private/helper methods, problem solved.

Philip Kendall
  • 22,899
  • 9
  • 58
  • 61
  • 4
    I'm not sure why this is being downvoted, but I'd agree with this answer. Eliminate the tests for private methods and move the testing to the public interface. Craft your test cases for the public interface around what is needed to fully exercise the private methods to get the coverage that you need. – Thomas Owens Sep 14 '21 at 14:49
  • 4
    And if you can't do the above, my experience is that it's because your class is too big and should actually be multiple classes. – Philip Kendall Sep 14 '21 at 16:05