Anand03
Anand03

Reputation: 223

Junit test case :: external web-service calls

I am trying to write a junit test case for my service layer class.

These methods in this service layer class has call to an external web-service which is deployed on a separate machine.

I want to make sure that the test methods are executed only when this external web-service is up and running.

Is there any convenient way to achieve this?

Regards

Upvotes: 0

Views: 1351

Answers (1)

radar
radar

Reputation: 605

If you can, I would try to avoid referencing the web-service all together. In unit testing it's normally a good idea to avoid database references, web-service calls, http, etc. since they are

  1. Slow
  2. Not consistent (as you said, the web service may be down)
  3. Not part of this "unit" - they should be tested separately

I'm not sure how your calls are being done, but look into mocking (with something like mockito). If you are able to mock the object that calls your web service, you can just tell it to return fake values (or do nothing), and then just verify that the web service was called. This way you are sure that your part of the code is working correctly, and as long as the web service is working correctly, then you should be good to go.

If you definitely need to access it, you can use try/catches to see if it times out, and only run the rest of the test if it doesn't do so. This way the catch will let the test pass without failing if the service is down.

Hope this helps

Upvotes: 1

Related Questions