Reputation: 53
In the client-server architecture, what should be the best approach when one should mock the client and when one should mock the server. I understand that unit tests should only test given class, with every dependent object mocked, while integration tests should test a feature as a whole. When it comes to the API calls, I am puzzled should I mock the client that I use for api calls or should I use some server mocking framework and let the real client call the mock server.
I have a situation where I should (it is not mandatory) test if I hit the proper API url, with proper method and with certain values passed in query parameters or in request body. With client mocking, I can simply "verify" if given parameters (path, method, request body) are passed and consider test successful. If I am to mock the server, then I need to create a little bit of processing on mock server to check if path, method and request body are correctly passed and return a value. In this case, server response is the only thing that I can use to measure if test is successful (send 500 if required data is not received).
By your experience, what is the proper way to test calls in client-server architecture for integration testing? If we go by the book, then server mocking should be used, but is that practical in the above mentioned case? For unit testing it is clear that mocks should be used.
Update: To avoid confusion: I am making a client that depends on the publicly available service which I cannot control. So, I am testing if the client is working properly and focus is on the client. Question is related should I (in integration-tests) mock the client object that is responsible for remote connections(In my case WSClient from Play framework), or should I use some server mock (like okhttp mock web server).
Another update: Most people suggest that simple rule of the thumb should be followed: if these are the unit tests then mock the client and if these are the integration tests then use mock server. Now, I am curious about this situation: I have a class A that has dependency on class B and class B has a client object that is tasked with remote calls. If I was to write unit test for class A, I should mock the class B and that is fine. However, if I am doing integration testing for class A, is it acceptable to mock client object in class B and "verify" if proper parameters are passed to it (path, method and request body), or, for the sake of completeness, I should run the mock server even if mocking the client object is easier. In this case, I will not test for timeouts and low level network errors. Or the whole point of integration testing, in this case, should be to test network connectivity as well.
Upvotes: 3
Views: 2403
Reputation: 642
You need:
Upvotes: 4