1

I have a suite of microservices:

  • ConnectorRegistrar
  • ConnectorTypeA
  • ConnectorTypeB
  • ...
  • ConnectorTypeZ

"Someone" can make a call to the ConnectorTypeA microservice to create a connector of type A, and it is needs to be known that "Someone" is the owner of that connector.

When ConnectorTypeA is created, it needs to register itself with ConnectorRegistrar.

The obvious option is to have ConnectorTypeA make an API call to ConnectorRegistrar to let it be known that "Someone" owns ConnectorTypeA.

The dilemma that I'm facing is I am not sure how to unit test this.

When we do the unit test for ConnectorTypeA creation, it will mock a creation through a database transaction. But if the API call is making another API call to the Registrar service, the Registrar will be making a record as well, which is wrong... as the unit test shouldn't trigger a permanent record.

Can someone please help me and point me in the right direction?

Thank you

  • 3
    you need to isolate your code; everything not being tested needs to be mocked, shimmed etc. google mocks for unit testing; Isolating code with mocks; etc. – esoterik Jun 15 '18 at 1:06
3

When ConnectorTypeA is created, it needs to register itself with ConnectorRegistrar

When ConnectorTypeA is created, it needs to be given a means of registering itself with something else. What that something else is is of no concern of ConnectorTypeA. In other words, it registers itself via an injected abstraction. So within your test, you provide it with some sort of mock of ConnectorRegistrar. In the real code, a real instance of ConnectorRegistrar is injected into it.

| improve this answer | |
  • Easy as that ;) Straight to the point. – Thomas Junk Jun 15 '18 at 7:52
4

You are confusing unit test with integration tests:

Unit tests

You want to test your ConnectorTypeA. Meaning you only care for ConnectorTypeA output given certain inputs.

If it calls ConnectorRegistrar when processing the input, in your test you will need to mock the HTTP response that ConnectorRegistrar would return for that particular test case.

Integration test

In your example, you mentioned a database. That's an integration between the service and the database. If this is what you actually want to do, you would need to fire up an instance of your ConnectorRegistrar service, and make sure it has the fixture data necessary to return what your ConnectorTypeA expect for its test case

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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