Tips and Best Practices for Effective Usage of Stubby4j in API Development

Jirawan.C
KBTG Life
Published in
4 min readJun 6, 2023

Today, I will walk you through an example of using stubby4j and provide a step-by-step guide on how to implement it. By following these instructions, you’ll be able to quickly set up and run stubby4j for API development.

Let’s get started!

Stubby is Stub server through HTTP/ HTTPS like a real API Server, 4J might stand for “For Java” because it was developed from Java. It’s commonly used in scenarios where simulating API endpoints is required, such as during API development, integration testing, load testing, or offline development. It provides a convenient way to stub out services and mimic their behavior, enabling more efficient and independent development and testing processes.

Here’s a step-by-step guide to get you started with Stubby4j, a stub server for testing HTTP-based services.

Step 1: Download and Install Stubby4j

npm install -g stubby

If you don’t have npm (Node Package Manager) installed, visit this link to see the instruction

Step 2: Create YAML Configuration File

  • Create a YAML configuration file (e.g., stubs.yaml) to define your stubs and their responses
  • Specify the desired routes, HTTP methods, request/response headers, and bodies in the YAML file
  • Save the configuration file

The url field is set to ^/todos$, which uses the ^ anchor to match the start of the URL and the $ anchor to match the end of the URL. This ensures that the URL is exactly /todos

The method field is set to GET to match only GET requests to this URL

The response section specifies the desired response when the URL is matched

The above code snippet is a configuration example for Stubby4j, which sets up an API endpoint for fetching TODOs.

Here’s a breakdown of the configuration:

  • description: provides a description of the API. In this case, it states that the API will show TODOs.

Request configuration:

  • url: specifies the URL pattern that the API endpoint should match. In this case, the URL pattern is ^/todos$, which uses regular expressions. It matches requests with the exact URL /todos (start and end anchors ensure exact match).
  • method: specifies the HTTP method for which this configuration should be applied. Here, it is set to GET, indicating that this configuration is for GET requests to the /todos endpoint.

Response configuration:

  • status: sets the HTTP response status code. In this case, it is set to 200, indicating a successful response.
  • latency: specifies the response delay in milliseconds. Here, it is set to 5000, meaning the response will be delayed by 5 seconds.
  • headers: defines the response headers. In this example, it sets the Content-Type header to application/json.
  • file: specifies the file path that contains the response body. Here, it is set to mock/todos.json, indicating that the response body will be read from the todos.json file.

Step 3: Start Stubby4j

  • Open a command prompt or terminal window
  • Navigate to the directory where you create project
  • Run the following command to start Stubby4j: java -jar stubby4j.jar -d stubs.yaml for java or stubby --data stubs.yaml for node JS
  • Stubby4j will read the configuration file and start the stub server on the default port 8882 (or a port specified in the configuration)

Step 4: Test Your Stubs

  • Open a web browser or use a tool like cURL or Postman to send requests to the stub server
  • Send requests to the configured routes and verify that Stubby4j responds with the expected stubbed responses
The response time is determined by the specified latency in stubs.yaml
The response time is determined by the specified latency in stubs.yaml
The example of response create a todo 3

We hope this article serves as a source of inspiration for improving your work processes and reduce waiting time for implementation within the team. If you find this knowledge valuable, we kindly ask you to show your appreciation by clicking the “clap” 👏 icon below. Your support is greatly appreciated! Thank you for taking the time to read it.

Visit the Stubby4j GitHub repository for more information:

Source Code:

Reference:

Want to read more articles like this? There are so many insightful contents waiting for you at KBTG Life, so make sure to hit that “follow” button so that you don’t miss out!

--

--

Jirawan.C
KBTG Life

Hi, My name's JUGJIG. I working as a software engineer @Fintech company. I’m Go developer since 2020. I’m English learner. Nice to see you