-1

In my project, I have two type of Service Class :

  • The simple one, they are interface that use the DAOs
  • The complicated one, are Rest Api Service

The Rest Service will make call to an API, and map the resulted JSON file as a mapped Object. Till here, there is no problem.

But, the thing is, that I had to make Unit Test of my RestApiService.

I have to explain one last thing, before asking my question. In my Rest API Service, I have multiple private methods that does some stuff specific to the API (such as finding request parameters from a file, sort them, filter them, etc...)

Should I make unit test of these private method? Or should I just make an Unit Test of the public method, that will call them?

On one hand, I feel like the Service should be like a contract, I ask it to give me mapped object and I should not care what it does.
On the other hand, I feel like, I should make unit tests of these 5 little private method.


The question How do you unit test private methods? is helpful, but in my situation, my Service contain multiple methods, and only one public method, should I still not unit test private methods?

EDIT

The second answer was actually the answer I needed, if my private method are so complex, I should create another Class for them. thanks

  • @gnat I want to add one detail, to why I think, my question add something, I will edit the question – Hamza Ince Aug 27 '19 at 12:35
  • "The question How do you unit test private methods? is helpful, but in my situation, my Service contain multiple methods, and only one public method, should I still not unit test private methods?" Test those private methods, but only trough the public one. Those private methods are implementation details and you may at some stage choose to completely change them. If you test through the public method, then a failing test means you messed up in your refactor. Test those private methods though and you have brittle tests that will fail because the details changed, not the result. That's bad. – David Arno Aug 27 '19 at 12:41
  • It makes sense, thank you for your time – Hamza Ince Aug 27 '19 at 12:44
0

To test the actual service, set up a fake API locally.

Let's say the restful api is /Api/Method1. Then create a server and a GET/POST to /Api/Method using express or something similar to return the JSON. Then you can run unit tests locally on all the methods, even the one that calls the restful API. Basically, your tests are replacing the real API with the local fake one return JSON data that looks real but comes from a fake source. Typically, in this case I would run the real service, capture the JSON, and then store this on the fake service and then return the same JSON.

There are still no dependencies here (other than your local fake API). This is also useful if you want to run demos since your application can be run locally with zero dependencies. So it can be run off network, etc.

Setting up fakes can be useful when you want to create unit tests for actual services.

|improve this answer|||||

Not the answer you're looking for? Browse other questions tagged or ask your own question.