OpenAPI Examples Simplified: Visualize and Generate Domain-Specific Test Data​

Share this page

Streamlining API Development: An Interactive Guide to Example Generation and Validation using Specmatic 

A robust, streamlined approach to API development is crucial for maintaining efficiency, reliability, and scalability in your development pipeline. One powerful methodology that has emerged is Contract-Driven Development (CDD), allowing developers to fully realize the benefits of microservices architectures. This article demonstrates how interactive example generation, validation, and service virtualization can be leveraged seamlessly using API specifications with Specmatic. 

The Foundation: API Specifications and Service Virtualization 

Let’s say we have an OpenAPI specification encompassing several operations such as finding available products, creating and retrieving orders, and more. This set of operations doesn’t include inline examples, critical for generating meaningful stub data for testing purposes. Specmatic’s interactive example generation makes the process simple and highly efficient. 

Interactive Example Generation: Bridging the Gaps 

When faced with an API specification devoid of inline examples, the first step is generating these examples. Specmatic includes an interactive examples generator with a user-friendly UI. This interface reflects the operations defined in the API specification, providing a clear visualization for developers. The absence of examples is quickly remedied by Specmatic’s ability to generate them interactively, adhering strictly to the specification’s parameters. 

Editing and Validating Generated Examples 

In a typical scenario developers might need to tweak the automatically generated examples to fit specific testing contexts. This process is straightforward within Specmatic’s framework. Crucially, modifications are validated and if an example fails to comply with the API’s constraints it is flagged as non-compliant. Immediate feedback on such issues is invaluable, ensuring that deviations from the specification are caught early in the development cycle. 

Starting the Stub Server: Bringing Examples to Life 

Post-validation, it is simple to launch a stub server using Specmatic. With the enriched examples saved under the _examples directory, the stub server activates, ready to handle requests. Using Postman, we can confirm that the server responds with the exact values previously set, verifying that the stub data accurately reflects the specified operations. This process significantly enhances developers’ ability to mock services, test integrations, and simulate real-world usage without relying on actual service endpoints. 

Injecting Domain Context with Dictionary JSON 

A critical element in generating meaningful stub responses is the utilization of a dictionary.json file. This file provides domain-specific clues, enabling Specmatic to populate response fields with relevant data, surpassing the generic placeholders often seen in auto-generated examples. This layer of contextual understanding ensures that stub data and test scenarios are not only valid but also relevant and aligned with business logic. 

Repurposing Examples for Testing 

In a powerful demonstration of Specmatic’s versatility, it can use examples for stub data as well as leveraging them for test data. By simply modifying a command to specify ‘test’ instead of ‘stub’, Specmatic can run comprehensive tests against a live application. This capability is pivotal in validating whether the application responses conform to the API specification and the generated examples. 

Upon executing the test, we can see multiple test outcomes, including failures. Through Specmatic’s interactive UI, these failures can be meticulously analyzed, offering deep insights into the root causes. This ensures that any divergence from expected behavior is promptly addressed. 

Conclusion: The Power of Executable API Contracts 

We have showcased the value of interactive, example-driven API development and the transformative impact of utilizing robust specifications throughout the lifecycle of microservices.  

Specmatic exemplifies how API specifications can be used as executable contracts, streamlining the entire development and testing lifecycle. Maintaining the specification in a central Git repository ensures consistency and collaboration among team members, whether they are API providers or consumers. This approach not only reinforces contract-driven development but also fortifies the reliability and predictability of microservices-based architectures. 

By embracing tools like Specmatic, developers can enhance their workflows, reduce errors, and ensure the resilience of their APIs from the ground up.

Related Posts

Specmatic vs WireMock

By Hari Krishnan

Comparison: Specmatic vs WireMock

API mocking is only effective if the mocks are truly representative of the provider / services they are emulating. Deviations between mocks and providers can
Read More
Specmatic vs Pact & Pactflow comparison

By Hari Krishnan

Comparison: Specmatic vs Pact.io and Pactflow.io

Specmatic and Contract Driven Development differ significantly from Pact. We built Specmatic out of our own necessity where Pact did not fit the bill for
Read More

By Naresh Jain

Contract Testing using OpenAPI Specs as Executable Contracts

Demonstration video showing OpenAPI specifications being leveraged as executable contracts with Specmatic Sample projects with OpenAPI https://docs.specmatic.io/sample_projects.html#openapi
Read More
Contract Testing with Specmatic - Markus Oberlehner

Stubbing an HTTP end-point with Specmatic

Markus Oberlehner gives a brief overview on the topic of Contract Testing with Specmatic and his experience using it for stubbing an HTTP end-point.
Read More

By Joel Rosario

TMForum ODA CTK API specification conformance testing with Specmatic

We recently discovered some major issues with the TMForum Conformance Test Kit (CTK) v5.0.0 and will demonstrate how using Specmatic can address these problems.
Read More
Specmatic API Coverage Report

Early detection of mismatches between your API specs and implementation: Exploring Specmatic’s API Coverage Report

Specmatic’s API coverage report helps identify any mismatches between an OpenAPI specification and an application's endpoints early in the development lifecycle. The report lists all
Read More
Dave Farley pointing to the Specmatic tool for easy microservice testing, featured on Dave Farley's Continuous Delivery channel.

“Easy Microservice Testing” – Specmatic featured on Continuous Delivery channel

Dave Farley is a respected author and widely recognised proponent of Continuous Delivery. In this video on his Continuous Delivery YouTube channel he discusses why
Read More
JDBC stubbing with Redis and Specmatic contract testing.

Break the Chains of Database Dependencies: Leveraging Specmatic for JDBC Stubbing

With Specmatic JDBC stub, you can easily test APIs without the need for a complex database setup. By switching out the real database with a
Read More

JMS Mocking with AsyncAPI using Specmatic

The JMS mock is wire compatible and can be controlled entirely from within the test. This means you can run the test locally or also
Read More
Specmatic + Kafka demo video thumbnail

Kafka Mocking with AsyncAPI using Specmatic

The Specmatic Kafka mock is wire compatible and entirely within the control of the test, the test can run locally and in CI and deliver
Read More