Mock a physical web server while junit test running

2.4k Views Asked by At

I am searching for a simple framework which is able to mock JSON responses for specific URLs and therefore starts an in-memory server. E.g. the framework could start a node.js server or similar.

I need a physical server since the unit tests are running against an external application (using selenium) and this application has a dependency to a JSON interface. The application itself is iPhone-App running inside of simulator and communicates to a REST interface.

Is there an existing framework for this or what would be the best approach given that I need to execute the tests with jUnit.

2

There are 2 best solutions below

3
On BEST ANSWER

You can start a stub server, given that you can configure the JSON URL in your client application.

Have a look for example at:

You can start these in your maven build during the pre-integrationtest phase and shut them down in post-integration test phase.

0
On

You can use npm package stubby-db. What you need to do is;

  1. Install : npm install stubby-db -g
  2. Mapping : Create a request response mapping which is quite easy. Specify the path of response file having JSON response.
  3. Run the stub server: stubbydb

That's all. However since you just want to run it for your unit tests only which doesn't require much performance, you can also use wiremock. It starts internally, and you never feel like you are running an external application.