Class GrpcServerRule
- java.lang.Object
-
- org.junit.rules.ExternalResource
-
- io.grpc.testing.GrpcServerRule
-
- All Implemented Interfaces:
org.junit.rules.TestRule
public final class GrpcServerRule extends org.junit.rules.ExternalResource
GrpcServerRule
is a JUnitTestRule
that starts an in-process gRPC service with aMutableHandlerRegistry
for adding services. PreferGrpcCleanupRule
in new code.GrpcServerRule
is useful for testing gRPC-based clients and services. However, becauseGrpcServerRule
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 usingGrpcServerRule
. Little benefit comes from proactively migrating existing code fromGrpcServerRule
, but new code is better served by explicit channel and server creation withGrpcCleanupRule
managing resource lifetimes.An
AbstractStub
can be created against this service by using theManagedChannel
provided bygetChannel()
.
-
-
Constructor Summary
Constructors Constructor Description GrpcServerRule()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected 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.GrpcServerRule
directExecutor()
ManagedChannel
getChannel()
Returns aManagedChannel
connected to this service.Server
getServer()
Returns the underlying gRPCServer
for this service.String
getServerName()
Returns the randomly generated server name for this service.MutableHandlerRegistry
getServiceRegistry()
Returns the service registry for this service.
-
-
-
Method Detail
-
directExecutor
public final GrpcServerRule directExecutor()
Returnsthis
configured to use a direct executor for theManagedChannel
andServer
. This can only be called at the rule instantiation.
-
getChannel
public final ManagedChannel getChannel()
Returns aManagedChannel
connected to this service.
-
getServerName
public final String getServerName()
Returns the randomly generated server name for this service.
-
getServiceRegistry
public final 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
-
-