In today's world of micro-services we typically integrate to many REST API (working on HTTP or HTTPS) for our application. For development and testing we need an environment to work with those API. For this typically we write stubs or create a test-web-server to simulate those API.
Hoverfly is one such tool that allows us to have API simulation. Take a look at the documentation of the tool in the standalone (or command-line) mode (Check out the basic tutorial). Its pretty straight forward and simple.
For development it may be feasible to use the command-line tool, but for the automated tests in the build process having this setup may be a bit of effort to setup the environment and use it.
Instead Hoverfly also has Java (and Python) bindings for the tool that we can use in the automation.
Lets look at a sample mock API example using Java.
We will be using maven to create a new application and add the following dependency
<dependency> <groupId>io.specto</groupId> <artifactId>hoverfly-java</artifactId> <version>0.12.0</version> </dependency>
Also make sure you are setting the Java compiler version in the properties
<properties> <!-- other properties, if any --> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
Now lets use hoverfly to capture and store the requests :
// Initialize Hoverfly instance try (Hoverfly hoverfly = new Hoverfly(HoverflyConfig.localConfigs().captureHeaders("Content-Type", "Date"), HoverflyMode.CAPTURE)) { // Start hover-fly hoverfly.start(); // Create a client to execute http requests. OkHttpClient comes bundled with Hoverfly. You can use any http-client. OkHttpClient client = new OkHttpClient(); Request dateReq = new Request.Builder().url("http://date.jsontest.com").build(); try (Response response = client.newCall(dateReq).execute()) { System.out.println("REAL : " + response.body().string()); } catch (IOException e) { e.printStackTrace(); } // Save/Export the response to a file hoverfly.exportSimulation(Paths.get(System.getProperty("user.dir"), "capture_requests.json")); }
You can check the capture_requests.json to view the request-response that got saved.
Now lets simulate the request
// Initialize Hoverfly in what they call SPY mode try (Hoverfly hoverfly = new Hoverfly(HoverflyConfig.localConfigs(), HoverflyMode.SPY)) { hoverfly.start(); // Initialize client to making http request OkHttpClient client = new OkHttpClient(); // As we have not yet imported any simulation, we will get the response from the real API call Request timeReq = new Request.Builder().url("http://time.jsontest.com").build(); Request dateReq = new Request.Builder().url("http://date.jsontest.com").build(); try (Response response = client.newCall(timeReq).execute()) { System.out.println("REAL : " + response.body().string()); } try (Response response = client.newCall(dateReq).execute()) { System.out.println("REAL : " + response.body().string()); } // Now lets import the simulation. // Remember we had captured 'date.jsontest' request which will give simulated responss. Any other requests // will still call the real API and give the response hoverfly.simulate(SimulationSource.file(Paths.get(System.getProperty("user.dir"), "capture_requests.json"))); try (Response response = client.newCall(timeReq).execute()) { System.out.println("REAL : " + response.body().string()); } try (Response response = client.newCall(dateReq).execute()) { System.out.println("SIMULATED : " + response.body().string()); } }
Above we used captured request simulation but you can easily build your own simulation quickly using what they call 'dsl'
So lets start Simulating :)