Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test and mock a GRPC service written in Java using Mockito

The protobuf definition is as follows:

syntax = "proto3";

package helloworld;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

I need to use Mockito along with JUnit testing.

like image 234
Shreshtha Garg Avatar asked Dec 23 '22 08:12

Shreshtha Garg


1 Answers

The encouraged way to test a service is to use the in-process transport and a normal stub. Then you can communicate with the service like normal, without lots of mocking. Overused mocking produces brittle tests that don't instill confidence in the code being tested.

GrpcServerRule uses in-process transport behind-the-scenes. We now I suggest taking a look at the examples' tests, starting with hello world.

Edit: We now recommend GrpcCleanupRule over GrpcServerRule. You can still reference the hello world example.

like image 175
Eric Anderson Avatar answered Jan 13 '23 15:01

Eric Anderson