I am currently trying to automate some of the testing for my application using JUnit. My application has a method which calls a 3rd party REST API. I need to check if that method is handling HTTP status codes like 404 without actually calling the 4rd party API.

e.g., My Method:

public int getNumUsers(){
    //call the 3rd party API - https://example.com/api/users/count
    //return user count

I need to test if the method getNumUsers is handling HTTP status code 404 which could be returned by the 3rd party API - https://example.com/api/users/count.

Any suggestions?


3 Answers 3


Use a mock.

Mock 3rd party API using MockServer or WireMock, if you're in the Java world.

  1. Your test will call getNumUsers() method
  2. which in turn will call mock of your 3rd API
  3. and the mock will simulate returning HTTP 404 response back to the getNumerUsers().
  4. Then your test will verify if the method can handle the 404 response, e.g., whether it throws an expected exceptions.
  • 1
    wiremock looks interesting to work with
    – PDHide
    May 10, 2020 at 6:28
  • 1
    Thanks for this JSON File configuration is really interesting
    – PDHide
    May 10, 2020 at 6:34
  • Thanks a lot! This was very helpful and answered my query! May 10, 2020 at 11:34

You don't need to add any additional dependency.

Start doing some preparatory refactoring in order to:

  1. Program to an interface instead of a concrete object;
  2. Break the getNumUsers() into two functions, one to fetch the data itself and another to extract the data from the DTO.

Then you can implement a stub which simulates the 404 return value.

(As a side-effect you will be able to create stubs for other behaviors this class straightforwardly)

  • I like this alternative direction. It will get faster tests: no need to setup HTTP server instance, no need to send HTTP request and wait for HTTP response. It may make sense for large number of such mocked tests. Obviously, for the price of not testing the actual HTTP traffic and HTTP client integration (hence serialization reqeust DTO to HTTP request and deserialization of HTTP response to response DTO).
    – dzieciou
    May 10, 2020 at 16:16

For anyone looking to know how to configure wiremock server using JSON

The below configuration creates an end point some/test using json

To use wiremock server:

usingFilesUnderDirectory() method will set the directory in which wiremock server needs to search for mappings and __Files folder. so make sure to keep the json file inside root/mappings/<endpoint>.json the name of the folder inside the root folder should be mapping and __files.

import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import com.github.tomakehurst.wiremock.WireMockServer;

WireMockServer wm = new WireMockServer(options().port(2345).usingFilesUnderDirectory("D:/temp/root"));

//Start the server and do something

    RequestSpecification httpRequest = RestAssured.given();
    Response response = httpRequest.get("http://localhost:2345/some/test");

//stop the service

Now keep the sample json under mappings folder under D:/temp/root

Sample Json:

  "id" : "5158b37b-3141-4b5f-ab45-06375dbbe658",
  "request" : {
    "url" : "/some/test",
    "method" : "GET"
  "response" : {
    "status" : 404,
    "body" : "Hello sdsworld!",
    "headers" : {
      "Content-Type" : "text/plain"
  "uuid" : "5158b37b-3141-4b5f-ab45-06375dbbe658",
  "persistent" : true,
  "insertionIndex" : 0

You can use any HTTP request library in Java (i hope you are using Java) or the language you are using in.

Currently in Java rest-assured and karate are two famous rest api testing utilities and i prefer these to even for intermittent validation as you can use it in future too when more complicated scenarios come up:

Install rest-assured using Maven:


IN your case for simple get request only rest-assured is required


Else for complex JSON parsing add the other required tools as mentioned in the documentation using Maven

import rest assured packages:

import io.restassured.RestAssured;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

Now do a simple get: https://www.toolsqa.com/rest-assured/rest-api-test-using-rest-assured/ (Provides great tutorials).

public int getNumUsers(){

         RequestSpecification httpRequest = RestAssured.given();
    RequestSpecification httpRequest = RestAssured.given();
    Response response = httpRequest.get("http://localhost:4535/some/test");


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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