How do mocking external services improve unit tests?

I connect to a simple, if idiosyncratic, external service.

I believe that my unit tests should not depend on the availability or implementation of this external service, so I intend to mock it.

I need a layout to receive and return realistic messages and replies - otherwise my tests will not represent the real state of affairs. For example, he must throw the correct errors - and there are at least 7 different ways in which he can fail (between you and me it is not a very well-designed external service). Therefore, with a minimum minimum, I should have a hash of message / response pairs.

Thus, instead of reducing unforeseen circumstances, the mockery again introduced him to another place. In fact, as they say, now I have two problems: I have to be sure that my hash has a fair idea of ​​how the external service behaves. But, of course, the canonical source of which response object X transmits message m, is X itself. Everything else is dangerous and random.

Did I make a wrong turn? How can I eliminate this apparent circularity?

EDIT I clarified that, in my opinion, the problem is in the light of useful comments.

+6
testing mocking
Feb 17 '09 at 22:51
source share
3 answers

Let me refer you to my two answers on another unit testing question , to avoid repeating myself.

What I think mockery gives you in this environment is that it strictly indicates what you think about the behavior of this external interface. This means that you have a controlled test (something tells me that this external service changes so often.) Thus, you can not only test and debug your code with a known "good" sequence of answers, but you have a documented one a set of examples of what you expect.

If I were in this situation and depending on the real service, I would have the desire to write unit test or mock for an external service. Thus, if you notice an error in real work, you can (1) run a test against your code using the layout for the external interface and (2) check the external service for your expectations.

The point, however, is to have something for which you have real confidence and that you are in full control.

+3
Feb 17 '09 at 23:20
source share

Your unit test layout does not have to represent an external service exactly. You must select predefined sets of input and output values ​​for the mocking external service. They may or may not be what the external service will actually return (but they must be "real"). The purpose of your unit tests is to double check that your objects are behaving correctly, given this set of inputs and outputs, for a specific value of "correct."

0
Feb 17 '09 at 22:56
source share

Make sure that everything your class depends on has an interface (as opposed to a specific implementation), and then you can use JMock . This will simplify your testing. You can do things like tell him, expect a method X to be called, return a specific value, or throw an exception. It really is time.

-one
Feb 17 '09 at 22:57
source share



All Articles