Home > Net >  Does GRPC have a Channel that can be used for testing?
Does GRPC have a Channel that can be used for testing?

Time:02-10

I'd like to create a simple test but with RPC within the same JVM.

What I was thinking was something like

@Test
void foo() {
    Channel channel = new SomeMagicalObject(new MySystemImpl());
    SystemStub stub =  SystemGrpc.newStub(channel);

    var ret = stub.doSomething(...)
    assertThat(ret).isTrue();
}

I was wondering if there's something already built that implements Channel that takes in a GRPC server implementation. That way I avoid having to run Netty and manage ports etc. Like this...

private static Server server;
private static Channel channel;

@BeforeAll
@SneakyThrows
static void setupServer() {
    server = ServerBuilder.forPort(0)
            .addService(new MySystemImpl())
            .build()
            .start();
    channel = ManagedChannelBuilder.forAddress("localhost", server.getPort())
            .usePlaintext()
            .build();

}

@AfterAll
@SneakyThrows
static void teardownServer() {
    server
            .shutdown()
            .awaitTermination();
}

CodePudding user response:

  •  Tags:  
  • Related