The most reliable way to test a gRPC service in Spring Boot is to start it on an in-process transport with grpc-java and call it through a real generated stub. For pure logic you can also call the service method directly with StreamRecorder from grpc-testing, with no network at all.
Both approaches exercise real protobuf serialization and gRPC status handling, which is what makes them better than mocking the stub. The examples assume a Greeter service generated from a .proto file with a unary SayHello(HelloRequest) returns (HelloReply) method.
Add the gRPC testing and in-process modules next to your existing gRPC and Spring Boot test dependencies:
grpc-inprocess ships with grpc-core in current grpc-java releases, so InProcessServerBuilder and InProcessChannelBuilder need no extra artifact. Use the same grpc-java version as the rest of your build.
StreamRecorder is a StreamObserver that records everything the service sends. This is the fastest test and needs no server.
This tests the method body but not interceptors, metadata or serialization.
An in-process server runs the full gRPC stack inside the JVM without opening a port, so tests are fast and cannot collide on ports.
GrpcTestConfig is a small @Configuration that component-scans only your service and its collaborators. The test builds the server itself, so it works whichever gRPC starter you use in production. For interceptors, call ServerInterceptors.intercept(greeterService, interceptor) before addService.
Older guides use GrpcCleanupRule, a JUnit 4 rule. With JUnit 5, close the channel and server in @AfterEach as shown, or use JUnit's @RegisterExtension with your own extension.
When you want the complete Spring context, the production server configuration and real network transport, start the app on a random port and connect a channel to it. The property names depend on your starter, so check its documentation for how to set the port to 0 and how to read the chosen port; the third-party net.devh starter uses grpc.server.port, and the official Spring gRPC project uses spring.grpc.server.port.
Use this sparingly. It is slower, and an in-process test covers most behaviour.
Replace repositories and clients with @MockitoBean (Spring Framework 6.2 and Spring Boot 3.4 and later; older versions use @MockBean) so the test controls the service's dependencies. For server-streaming methods, StreamRecorder.getValues() returns every message in order; for client and bidirectional streaming, use the async stub with your own StreamObserver and a CountDownLatch, or StreamRecorder as the response observer.
Usually not. An in-process server with a real stub is simpler and tests more. Mock the dependencies behind the service instead.
Catch StatusRuntimeException from a blocking stub, or read getError() from StreamRecorder, and compare Status.fromThrowable(error).getCode().
directExecutor() in tests?It runs calls on the calling thread, which makes tests deterministic. Remove it if you specifically test concurrency.
Yes. Attach metadata with MetadataUtils.newAttachHeadersInterceptor and set a deadline with stub.withDeadlineAfter(1, TimeUnit.SECONDS).
Test gRPC services in Spring Boot with StreamRecorder for fast unit tests and an in-process server plus a generated stub for realistic integration tests. Assert on Status codes, mock only the collaborators, and reserve a real-port test for full-stack checks.