Class GrpcServerRule

  • All Implemented Interfaces:
    org.junit.rules.TestRule

    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. 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().

    • Constructor Detail

      • GrpcServerRule

        public GrpcServerRule()
    • Method Detail

      • directExecutor

        public final GrpcServerRule directExecutor()
        Returns this configured to use a direct executor for the ManagedChannel and Server. This can only be called at the rule instantiation.
      • getServer

        public final Server getServer()
        Returns the underlying gRPC Server for this service.
      • getServerName

        public final java.lang.String getServerName()
        Returns the randomly generated server name for this service.
      • after

        protected void after()
        After the test has completed, clean up the channel and server.
        Overrides:
        after in class org.junit.rules.ExternalResource
      • before

        protected void before()
                       throws java.lang.Throwable
        Before the test has started, create the server and channel.
        Overrides:
        before in class org.junit.rules.ExternalResource
        Throws:
        java.lang.Throwable