Class GrpcServerRule
- All Implemented Interfaces:
org.junit.rules.TestRule
GrpcServerRule
is a JUnit TestRule
that starts an in-process gRPC service with
a MutableHandlerRegistry
for adding services. Prefer GrpcCleanupRule
in new code.
GrpcServerRule
is useful for testing gRPC-based clients and services. However,
because GrpcServerRule
does not support useful features such as transport
types other than in-process, multiple channels per server, custom channel or server builder
options, and configuration inside individual test methods, users would end up to a difficult
situation when later they want to make extensions to their tests that were using
GrpcServerRule
. Little benefit comes from proactively migrating existing code from
GrpcServerRule
, but new code is better served by explicit channel and server creation with
GrpcCleanupRule
managing resource lifetimes.
An AbstractStub
can be created against this service by using the
ManagedChannel
provided by getChannel()
.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate io.grpc.ManagedChannel
private io.grpc.Server
private String
private io.grpc.util.MutableHandlerRegistry
private boolean
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected void
after()
After the test has completed, clean up the channel and server.protected void
before()
Before the test has started, create the server and channel.final GrpcServerRule
Returnsthis
configured to use a direct executor for theManagedChannel
andServer
.final io.grpc.ManagedChannel
Returns aManagedChannel
connected to this service.final io.grpc.Server
Returns the underlying gRPCServer
for this service.final String
Returns the randomly generated server name for this service.final io.grpc.util.MutableHandlerRegistry
Returns the service registry for this service.Methods inherited from class org.junit.rules.ExternalResource
apply
-
Field Details
-
channel
private io.grpc.ManagedChannel channel -
server
private io.grpc.Server server -
serverName
-
serviceRegistry
private io.grpc.util.MutableHandlerRegistry serviceRegistry -
useDirectExecutor
private boolean useDirectExecutor
-
-
Constructor Details
-
GrpcServerRule
public GrpcServerRule()
-
-
Method Details
-
directExecutor
Returnsthis
configured to use a direct executor for theManagedChannel
andServer
. This can only be called at the rule instantiation. -
getChannel
public final io.grpc.ManagedChannel getChannel()Returns aManagedChannel
connected to this service. -
getServer
public final io.grpc.Server getServer()Returns the underlying gRPCServer
for this service. -
getServerName
Returns the randomly generated server name for this service. -
getServiceRegistry
public final io.grpc.util.MutableHandlerRegistry getServiceRegistry()Returns the service registry for this service. The registry is used to add service instances (e.g.BindableService
orServerServiceDefinition
to the server. -
after
protected void after()After the test has completed, clean up the channel and server.- Overrides:
after
in classorg.junit.rules.ExternalResource
-
before
Before the test has started, create the server and channel.- Overrides:
before
in classorg.junit.rules.ExternalResource
- Throws:
Throwable
-