StyleBender
StyleBender

Reputation: 51

How to use WireMock to mock an upstream service with Protobuf (gRPC) response

I am writing integration tests for my Java Service. We are thinking of using WireMock to mock upstream services. However, one of our upstream returns a Protobuf (grpc) response and I can't seem to understand how to mock a gRPC response using WireMock. I tried to convert the response to byteArray and use that ,however, wiremock fails to deserialize it to Protobuf Java Object.

We have a limitation that we can only use 1 mock server since all our upstream requests are forwarded via a servicemesh sidecar running on a fixed port.

Thanks!

Upvotes: 5

Views: 5239

Answers (2)

Tom
Tom

Reputation: 4149

WireMock now has an official extension that supports gRPC mocking.

It works by converting protobuf messages to and from JSON so that WireMock's JSON matching can be used as normal.

Matching and responses can be expressed directly as JSON, or Java classes generated by protoc can be used via the extension's DSL:

mockGreetingService.stubFor(
        method("greeting")
            .withRequestMessage(equalToMessage(HelloRequest.newBuilder().setName("Tom")))
            .willReturn(message(HelloResponse.newBuilder().setGreeting("Hi"))));

Upvotes: 0

Wojtek
Wojtek

Reputation: 1530

You could use Wiremock-gRPC or any of the other tools mentioned on the Wikipedia comparison site such as the commercial tool Traffic Parrot that supports gRPC or Camouflage.

Upvotes: 4

Related Questions