Contract Testing using gRPC Specs as Executable Contracts

By Yogesh Nikam

Share this page

Transform your gRPC API specs into executable contracts in seconds

Now you can easily leverage your gRPC APIs for contract testing, intelligent service virtualisation and backward compatibility testing, just like you would for your OpenAPI-based services. No more complex and brittle setups that involve an assortment of tools – Specmatic handles it all for you, thereby making for a one stop solution that enables a seamless DevEx. 

Streamlining Microservices Development with Contract-Driven Testing

In the evolving world of microservices, ensuring robust and reliable communication between services is no small feat. Services need to interact efficiently without introducing bottlenecks or faults that could lead to system failures. Specmatic is a powerful tool that simplifies the contract testing of gRPC services, thereby enhancing the resilience and dependability of microservice architectures.

Understanding the Architecture

Our application comprises three key components: the client, the Backend-for-Frontend (BFF) service, and the backend domain service. The glue binding these components together is the gRPC protocol, renowned for its performance and efficiency. Testing these components in isolation becomes paramount to ensure independent functionality and overall system stability.

Specmatic steps into this context to make the otherwise daunting task of contract testing straightforward and maintainable. In testing mode, Specmatic replaces the real client with a simulated client and the backend domain service with a gRPC stub, mimicking its interactions and responses.

The Power of Specmatic in Isolated Testing

In isolation testing, the aim is to test the BFF service without depending on the client or the backend domain service. This setup leverages Specmatic, which acts as a substitute for these components by emulating their behavior. By doing so, Specmatic effectively validates the interactions and ensures they comply with the defined contract.

Specmatic achieves this by generating a stub based on the API specification file of the backend domain service. This stub is a highly accurate representation, ensuring the BFF behaves correctly even in the absence of actual backend services. This results in faster and more efficient tests.

Exploring the gRPC Methods and Tests

Let’s dive deeper into the testing methodology by examining the gRPC methods and the corresponding tests. A typical RPC method such as create order expects a request containing a new order’s details and returns an order ID. Specmatic tests validate these interactions against the specified contract.

Valid Request Testing

When a valid request, adhering to all required fields and constraints, is sent to the BFF service, Specmatic ensures the response is validated correctly against the schema. If the responses conform to the expected contract, the tests pass seamlessly, confirming the service’s behavior.

Negative Scenario Testing

Specmatic also excels in testing negative scenarios, known as mutation-based testing. Consider a scenario where a request field such as count is below the minimum value defined by the contract. Specmatic expects an appropriate error response. If the service fails to handle this correctly and returns a valid response instead, Specmatic identifies the issue, flags the test, and highlights exactly where the problem lies.

This meticulous approach extends to other tests, such as missing required fields. Specmatic checks for the appropriate error handling, ensuring that every plausible negative scenario is rigorously tested.

Automated Test Generation and Execution

One of the standout features of Specmatic is its ability to auto-generate tests. Developers do not need to handcraft each test case; instead, Specmatic uses the API specifications to generate comprehensive tests covering various scenarios. This automation dramatically reduces the overhead and ensures no critical test cases are overlooked.

The Specmatic Configuration File

The real magic lies in the Specmatic configuration file, a YAML file detailing the specifications and configurations necessary for Specmatic to operate. This file specifies the specs for the BFF service and the domain services it depends on, guiding Specmatic’s test generation and stub creation processes.

Enhancing Microservices Reliability

The adoption of Specmatic in contract-driven development offers numerous benefits:

  • Early Detection of Integration Issues: By validating interactions early in the development cycle, integration issues are identified and resolved promptly.
  • Reduced Dependency on Live Services: Eliminates the need to run actual dependent services, thus simplifying the testing workflow.
  • Assurance of Compliance: Ensures all interactions adhere strictly to the agreed-upon API contracts, fostering consistency and reliability.
  • Boosted Developer Confidence: With exhaustive tests ensuring all scenarios are covered, developers gain increased confidence in the stability and resilience of the microservices.

Conclusion

Specmatic revolutionizes the way developers approach contract testing in gRPC services. Its ability to simulate clients and backend services, generate exhaustive tests, and validate interactions against strict contracts ensures that microservices can be developed, tested, and deployed with greater confidence. By maintaining API specifications centrally and automating test generation, Specmatic embodies the best practices of contract-driven development.

Getting Started

For gRPC, the setup process is just as straightforward as it is for OpenAPI. All you need are your gRPC proto files, along with a simple Specmatic configuration, and you’re good to go!

Sample gRPC projects

https://github.com/znsio/specmatic-order-bff-grpc-kotlin 
https://github.com/znsio/specmatic-order-api-grpc-kotlin 
https://github.com/znsio/specmatic-order-bff-grpc-go 

Give a star to Specmatic – https://github.com/znsio/specmatic  

Available in the Pro plan or higher

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