I have two services communicating with gRPC (one is gRPC server, and the other is gRPC client). I want to write a test for the gRPC client and want to mock the gRPC server.
I am trying to that by using wiremock-grpc-extension
with a simple Java Maven project.
- I have added the proto compiler maven plugin that generates sources for the proto file
- I have configured the wiremock with the following code:
static void init() {
WireMockServer server = new WireMockServer(
WireMockConfiguration.wireMockConfig()
.port(50051)
.withRootDirectory("src/test/resources/wiremock")
.extensions(new GrpcExtensionFactory())
);
server.start();
Runtime.getRuntime().addShutdownHook(new Thread(server::stop));
System.out.println("WireMock gRPC server is running at: " + server.baseUrl());
Helloworld.HelloWorldRequest.Builder requestBodyBuilder = Helloworld.HelloWorldRequest.newBuilder().setName("john").setSurname("doe");
Helloworld.HelloWorldResponse.Builder responseBodyBuilder = Helloworld.HelloWorldResponse.newBuilder().setMessage("Hello JD");
WireMockGrpcService mockService =
new WireMockGrpcService(
new WireMock(server.port()),
HelloWorldServiceGrpc.SERVICE_NAME
);
mockService.stubFor(
method("SayHello")
.withRequestMessage(equalToMessage(requestBodyBuilder))
.willReturn(message(responseBodyBuilder))
);
}
When I run the app and check the wiremock stubs from the web console, mocking configuration seems correct (see the below image)
However, when I run the app and call the SayHello
method from Postman with a gRPC request, I get “Method Not Found” error.
While I cannot call the mock server with gRPC request, I can call with an HTTP POST strangely. I am trying to mock a gRPC server not an HTTP endpoint.
I think something is wrong with my mock server configuration code but couldn’t figure out what. Any help is appreciated. Thank you in advance.