Class GrpcMock
- Author:
- Fadelis
-
Method Summary
Modifier and TypeMethodDescriptionstatic CountMatcheratLeast(int count) Called the specified number of times or more.static CountMatcheratMost(int count) Called the specified number of times or less.static <ReqT,RespT>
BidiStreamingMethodStubBuilderStep<ReqT,RespT> bidiStreamingMethod(io.grpc.MethodDescriptor<ReqT, RespT> method) Returns a stub builder forMethodDescriptor.MethodType.BIDI_STREAMINGmethod.static <ReqT> RequestPatternBuilderStep<ReqT>calledMethod(io.grpc.MethodDescriptor<ReqT, ?> method) Returns request pattern builder instance, used for verifying call count usingverifyThat(RequestPatternBuilderStep, CountMatcher).<ReqT> List<CapturedRequest<ReqT>>capturedRequestsFor(RequestPattern<ReqT> requestPattern) Return allCapturedRequestthat match the provided request pattern.static <ReqT> List<CapturedRequest<ReqT>>capturedRequestsFor(RequestPatternBuilderStep<ReqT> requestPattern) Return allCapturedRequestthat match the provided request pattern.static <ReqT,RespT>
ClientStreamingMethodStubBuilderStep<ReqT,RespT> clientStreamingMethod(io.grpc.MethodDescriptor<ReqT, RespT> method) Returns a stub builder forMethodDescriptor.MethodType.CLIENT_STREAMINGmethod orMethodDescriptor.MethodType.BIDI_STREAMINGmethod with a single response at request stream completion.static voidconfigureFor(int port) Configure the global static gRPC Mock instance to use a new one with the provided port.static voidconfigureFor(GrpcMock client) Configure the global static gRPC Mock instance to use the provided one.Returns a response action, which will send out the given exception viaStreamObserver.onError(java.lang.Throwable).static StringReturns the name ofInProcessServerif one is used for the global static gRPC Mock instance.static intReturns the port for the global static gRPC Mock instance.Retrieve the name ofInProcessServerif one is used.intgetPort()Retrieve current port of the server.static GrpcMockBuildergrpcMock()Returns gRPC Mock builder initiated with a random port.static GrpcMockBuildergrpcMock(int port) Returns gRPC Mock builder with the given port.static GrpcMockBuildergrpcMock(io.grpc.ServerBuilder serverBuilder) Returns gRPC Mock builder using the provided gRPCServerBuilderconfiguration.static InProcessGrpcMockBuilderReturns an in-process gRPC Mock builder by generating a random name.static InProcessGrpcMockBuilderinProcessGrpcMock(String name) Returns an in-process gRPC Mock builder with the given name.static CountMatchernever()Never called.<ReqT,RespT>
voidregister(MethodStubBuilder<ReqT, RespT> methodStubBuilder) Register a gRPC method stub to the server.voidresetAll()Removes all stubs defined from the mock server.static voidRemoves all stubs defined from the global mock server.static <RespT> ObjectResponseActionBuilder<RespT>response(RespT responseObject) Returns a response action, which will send out the given response object viaStreamObserver.onNext(V).static <ReqT,RespT>
ServerStreamingMethodStubBuilderStep<ReqT,RespT> serverStreamingMethod(io.grpc.MethodDescriptor<ReqT, RespT> method) Returns a stub builder forMethodDescriptor.MethodType.SERVER_STREAMINGmethod.start()Starts the gRPC mock server.statusException(io.grpc.Status status) Returns a response action, which will send out aStatusRuntimeExceptionwith givenStatusviaStreamObserver.onError(java.lang.Throwable).stop()Stops the gRPC mock server viaServer.shutdownNow().static <RespT> ObjectStreamResponseBuilderStep<RespT>Returns a stream response, which can respond with multipleResponseAction.static <RespT> ExceptionStreamResponseBuildersStep<RespT>stream(ExceptionResponseActionBuilder responseAction) Returns a terminating stream response, which will respond withResponseActionand terminate the call, since it will beStreamObserver.onError(java.lang.Throwable)response.static <RespT> ObjectStreamResponseBuilderStep<RespT>stream(ObjectResponseActionBuilder<RespT> responseAction) Returns a stream response, which can respond with multipleResponseAction.static <RespT> ObjectStreamResponseBuilderStep<RespT>stream(RespT... responses) Returns a stream response, which can respond with multipleResponseAction.static <ReqT,RespT>
voidstubFor(MethodStubBuilder<ReqT, RespT> methodStubBuilder) Register a gRPC method stub to the global gRPC mock server.static CountMatchertimes(int count) Called exactly the specified number of times.static <ReqT,RespT>
UnaryMethodStubBuilderStep<ReqT,RespT> unaryMethod(io.grpc.MethodDescriptor<ReqT, RespT> method) Returns a stub builder forMethodDescriptor.MethodType.UNARYmethod orMethodDescriptor.MethodType.SERVER_STREAMINGmethod with a single response.static <ReqT> voidverifyThat(io.grpc.MethodDescriptor<ReqT, ?> method) Verify that given method was called exactly once.static <ReqT> voidverifyThat(io.grpc.MethodDescriptor<ReqT, ?> method, CountMatcher countMatcher) Verify that given method was called number of times satisfying providedCountMatcher.<ReqT> voidverifyThat(RequestPattern<ReqT> requestPattern, CountMatcher countMatcher) Verify that givenRequestPatternis called a number of times satisfying the providedCountMatcher.static <ReqT> voidverifyThat(RequestPatternBuilderStep<ReqT> requestPattern) Verify that givenRequestPatternwas called exactly once.static <ReqT> voidverifyThat(RequestPatternBuilderStep<ReqT> requestPattern, CountMatcher countMatcher) Verify that givenRequestPatternwas called number of times satisfying providedCountMatcher.
-
Method Details
-
getPort
public int getPort()Retrieve current port of the server.- Throws:
IllegalStateException- if the server has not yet been started.
-
getInProcessName
Retrieve the name ofInProcessServerif one is used.- Throws:
GrpcMockException- if the server is not an in-process one.
-
start
Starts the gRPC mock server. Does not throw any exception if the server is already running.- Throws:
GrpcMockException- if the server is unable to start.
-
stop
Stops the gRPC mock server viaServer.shutdownNow(). -
register
Register a gRPC method stub to the server.
If given method is already registered, then configured scenarios will be appended to that method's stub.
-
verifyThat
public <ReqT> void verifyThat(@Nonnull RequestPattern<ReqT> requestPattern, @Nonnull CountMatcher countMatcher) Verify that givenRequestPatternis called a number of times satisfying the providedCountMatcher.- Throws:
GrpcMockVerificationError- if the verify step fails.
-
capturedRequestsFor
public <ReqT> List<CapturedRequest<ReqT>> capturedRequestsFor(@Nonnull RequestPattern<ReqT> requestPattern) Return allCapturedRequestthat match the provided request pattern. -
resetAll
public void resetAll()Removes all stubs defined from the mock server. -
grpcMock
Returns gRPC Mock builder initiated with a random port. -
grpcMock
Returns gRPC Mock builder with the given port. If given port is0, then a random free port will be selected. -
inProcessGrpcMock
Returns an in-process gRPC Mock builder with the given name. -
inProcessGrpcMock
Returns an in-process gRPC Mock builder by generating a random name. -
grpcMock
Returns gRPC Mock builder using the provided gRPCServerBuilderconfiguration. The user is responsible that the port used in the builder is available and free. -
configureFor
public static void configureFor(int port) Configure the global static gRPC Mock instance to use a new one with the provided port. -
configureFor
Configure the global static gRPC Mock instance to use the provided one. -
getGlobalPort
public static int getGlobalPort()Returns the port for the global static gRPC Mock instance.- Throws:
IllegalStateException- if the server has not yet been started.
-
getGlobalInProcessName
Returns the name ofInProcessServerif one is used for the global static gRPC Mock instance.- Throws:
GrpcMockException- if the server is not an in-process one.
-
resetMappings
public static void resetMappings()Removes all stubs defined from the global mock server. -
stubFor
Register a gRPC method stub to the global gRPC mock server.
If given method is already registered, then configured scenarios will be appended to that method's stub.
When multiple stubs, satisfying the same request condition matching, are registered, the last one registered will be triggered.
- Parameters:
methodStubBuilder- a method stub builder created through one ofunaryMethod(io.grpc.MethodDescriptor<ReqT, RespT>),serverStreamingMethod(io.grpc.MethodDescriptor<ReqT, RespT>),clientStreamingMethod(io.grpc.MethodDescriptor<ReqT, RespT>)orbidiStreamingMethod(io.grpc.MethodDescriptor<ReqT, RespT>).
-
unaryMethod
public static <ReqT,RespT> UnaryMethodStubBuilderStep<ReqT,RespT> unaryMethod(@Nonnull io.grpc.MethodDescriptor<ReqT, RespT> method) Returns a stub builder forMethodDescriptor.MethodType.UNARYmethod orMethodDescriptor.MethodType.SERVER_STREAMINGmethod with a single response. -
serverStreamingMethod
public static <ReqT,RespT> ServerStreamingMethodStubBuilderStep<ReqT,RespT> serverStreamingMethod(@Nonnull io.grpc.MethodDescriptor<ReqT, RespT> method) Returns a stub builder forMethodDescriptor.MethodType.SERVER_STREAMINGmethod. -
clientStreamingMethod
public static <ReqT,RespT> ClientStreamingMethodStubBuilderStep<ReqT,RespT> clientStreamingMethod(@Nonnull io.grpc.MethodDescriptor<ReqT, RespT> method) Returns a stub builder for
MethodDescriptor.MethodType.CLIENT_STREAMINGmethod orMethodDescriptor.MethodType.BIDI_STREAMINGmethod with a single response at request stream completion. -
bidiStreamingMethod
public static <ReqT,RespT> BidiStreamingMethodStubBuilderStep<ReqT,RespT> bidiStreamingMethod(@Nonnull io.grpc.MethodDescriptor<ReqT, RespT> method) Returns a stub builder for
MethodDescriptor.MethodType.BIDI_STREAMINGmethod. -
response
Returns a response action, which will send out the given response object viaStreamObserver.onNext(V). -
exception
Returns a response action, which will send out the given exception via
StreamObserver.onError(java.lang.Throwable).It not recommended to use this methods, because without a proper
ServerInterceptortranslating non-gRPC exceptions to gRPC ones It will be translated toStatus.UNKNOWNtype of exception without any message. ThestatusException(Status)should be used to define concrete gRPC errors. -
statusException
Returns a response action, which will send out a
StatusRuntimeExceptionwith givenStatusviaStreamObserver.onError(java.lang.Throwable). -
stream
public static <RespT> ObjectStreamResponseBuilderStep<RespT> stream(@Nonnull ObjectResponseActionBuilder<RespT> responseAction) Returns a stream response, which can respond with multiple
ResponseAction. -
stream
Returns a stream response, which can respond with multiple
ResponseAction.In order to configure a
Delayfor the actions seeresponse(RespT)method.- Parameters:
responses- single response objects for the stream response. Will be returned in provided list order.
-
stream
Returns a stream response, which can respond with multiple
ResponseAction.In order to configure a
Delayfor the actions seeresponse(RespT)method.- Parameters:
responses- single response objects for the stream response. Will be returned in provided array order.
-
stream
public static <RespT> ExceptionStreamResponseBuildersStep<RespT> stream(@Nonnull ExceptionResponseActionBuilder responseAction) Returns a terminating stream response, which will respond with
ResponseActionand terminate the call, since it will beStreamObserver.onError(java.lang.Throwable)response. -
verifyThat
Verify that given method was called exactly once.
This is the same as invoking
verifyThat(method, times(1)).- Throws:
GrpcMockVerificationError- if the verify step fails.
-
verifyThat
Verify that given
RequestPatternwas called exactly once.This is the same as invoking
verifyThat(requestPattern, times(1)).- Throws:
GrpcMockVerificationError- if the verify step fails.
-
verifyThat
public static <ReqT> void verifyThat(@Nonnull io.grpc.MethodDescriptor<ReqT, ?> method, @Nonnull CountMatcher countMatcher) Verify that given method was called number of times satisfying provided
CountMatcher.- Throws:
GrpcMockVerificationError- if the verify step fails.
-
verifyThat
public static <ReqT> void verifyThat(@Nonnull RequestPatternBuilderStep<ReqT> requestPattern, @Nonnull CountMatcher countMatcher) Verify that given
RequestPatternwas called number of times satisfying providedCountMatcher.- Throws:
GrpcMockVerificationError- if the verify step fails.
-
capturedRequestsFor
public static <ReqT> List<CapturedRequest<ReqT>> capturedRequestsFor(@Nonnull RequestPatternBuilderStep<ReqT> requestPattern) Return allCapturedRequestthat match the provided request pattern. -
calledMethod
public static <ReqT> RequestPatternBuilderStep<ReqT> calledMethod(@Nonnull io.grpc.MethodDescriptor<ReqT, ?> method) Returns request pattern builder instance, used for verifying call count usingverifyThat(RequestPatternBuilderStep, CountMatcher). -
times
Called exactly the specified number of times. -
never
Never called. -
atLeast
Called the specified number of times or more. -
atMost
Called the specified number of times or less.
-