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

support returning the requests for manual asserting #32

Closed christophsturm closed 11 months ago

christophsturm commented 1 year ago

I would like to verify my grpc requests with my assertion lib instead of using verify, something like

List<CapturedRequest> l = grpcMock.getRequestsFor(calledMethod(...))
assertThat(l...)

mainly to get better error messages and because I think asserting should be left to assertion libs.

Fadelis commented 12 months ago

Hi @christophsturm , this was done in a similar manner as is done in wiremock.

I could expose methods for returning the int count, but I wouldn't want to expose CapturedRequest structure as it has a lot of info and it would require a lot of redundant definitions in your assertion (ie. all the incoming headers)

christophsturm commented 12 months ago

the problem with the current approach is that when just one of my matchers is not met the assertion fails and i don't know what is wrong with the request.

for example a verify call like this:

                    .withHeader("authorization", GRPCUtils.isJwtToken)
                    .withRequest(r -> ..., times(1));

it just tells me that it was not called like this 1 times, but to track down the error i would like to see the actual request.

the workaround that I am currently using if I really want to assert on the request is this:

            // get the grpc requests for manual asserting:
            LinkedList<...> requests = new LinkedList<>();
            grpcMock.verifyThat(calledMethod(...).withRequest(requests::add)
                    .withHeader("authorization", GRPCUtils.isJwtToken).build(),
                    times(1));
            assertThat(requests).hasSize(1);
            assertThat(requests.get(0).getBlah()).isEqualTo("...");

but I think it would be best if I could just get a list of requests with headers for a called method.

Fadelis commented 11 months ago

@christophsturm fair enough, that sounds like a valid use case. I can introduce a method to returns the list of CapturedRequest.

christophsturm commented 11 months ago

perfect. why does it return a list of captured requests, and then each captured request again contains a list of requests? do I need to flat map them to get all the request?

Fadelis commented 11 months ago

perfect. why does it return a list of captured requests, and then each captured request again contains a list of requests? do I need to flat map them to get all the request?

CapturedRequest encapsulates all gRPC supported method types. That includes client streaming and bidi streaming methods, which can contain multiple requests in the same call.