@ExperimentalApi(value="https://github.com/grpc/grpc-java/issues/2488") public final class GrpcServerRule extends org.junit.rules.ExternalResource
GrpcServerRule
is a JUnit TestRule
that starts an in-process gRPC service with
a MutableHandlerRegistry
for adding services. It is particularly useful for mocking out
external gRPC-based services and asserting that the expected requests were made. However, due to
it's limitation that 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
. So in general it is more favorable to use the more flexible TestRule
GrpcCleanupRule
than GrpcServerRule
in unit tests.
An AbstractStub
can be created against this service by using the
ManagedChannel
provided by getChannel()
.
Constructor and Description |
---|
GrpcServerRule() |
Modifier and Type | Method and 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 a
ManagedChannel connected to this service. |
Server |
getServer()
Returns the underlying gRPC
Server for this service. |
String |
getServerName()
Returns the randomly generated server name for this service.
|
MutableHandlerRegistry |
getServiceRegistry()
Returns the service registry for this service.
|
public final GrpcServerRule directExecutor()
this
configured to use a direct executor for the ManagedChannel
and
Server
. This can only be called at the rule instantiation.public final ManagedChannel getChannel()
ManagedChannel
connected to this service.public final String getServerName()
public final MutableHandlerRegistry getServiceRegistry()
BindableService
or ServerServiceDefinition
to the server.protected void after()
after
in class org.junit.rules.ExternalResource