Fadelis / grpcmock

A gRPC Java testing tool to easily mock endpoints of gRPC services for IT or Unit testing
http://grpcmock.org
Apache License 2.0
144 stars 13 forks source link

How to mock Grpc Method .withRequest with one unknown field #17

Closed draugrmorar closed 2 years ago

draugrmorar commented 2 years ago

How to mock Grpc Method with request with one unknown field?

stubFor(unaryMethod(UserGrpc.getCreateMethod()) .withRequest(UserOuterClass.CreateUserReq.newBuilder() .setEmail(email) .setPassword( any() ) <--------- this field? .build() ) .willReturn(UserOuterClass.User.newBuilder() .setId(userId) .build() ) );

I tried Mockito's any() etc. It doesn't work

Fadelis commented 2 years ago

Hi @Radnaeva, this library does not use any of the Mockito's constructs, but even in Mockito, your usage of any() would be incorrect.

If you can't match your request object fully via .equals method, then you have to use the Predicate form of withRequest method. Then you can do one of the following:

  1. Match each individual property you're interested in:
    .withRequest(req -> req.getEmail().equals("some@email")
    && req.getName().equals("Some Name")) 
  2. Or remove properties that are not know from the request and match to your partial objects:
    .withRequest(req -> req.toBuilder().clearPassword().build().equals(CreateUserReq.newBuilder()
    .setEmail(email)
    .build()))