Integration Testing with a fake server

2019-07-22 06:11发布

问题:

I have a service which connects to a set of api's which i want to test.

I would like to make a Fake Service for my integration tests (to simulate failure situations)

I cannot simply use @RestController as they are not loaded during a test, and I looked into mockserver but I am unsure if it is what I am looking for, as I do not want my test to trigger a mock call, but rather my code should trigger the api call normally, simply using the mock server rather than an actual server (the base url is configurable so i can make my service point to a mock server)

UPDATE 1:

The only alternative I am seeing is possibly launching Cargo and deploying my service inside it?

UPDATE 2: I just found out about Wiremock. Could this be usable here?

回答1:

I went with WireMock http://wiremock.org/

Setting the ClassRule in a junit test will have the fake server up and running before your test starts.



回答2:

Mockito(http://docs.mockito.googlecode.com/hg/1.9.5/org/mockito/Mockito.html) is a good library for mocking method calls. It can return a certain value given a certain input. With this library you could mock a REST client to simulate the communication between your application and the REST api.