Spring / Spring gRPC Interview Questions
How do you unit test a gRPC service implementation without starting a real server?
Since a @GrpcService class is ultimately just a plain Java class implementing the generated interface, it can be instantiated directly in a unit test like any other object - no Spring context or network layer required.
GreetingService service = new GreetingService(mockDependency); TestStreamObserver<HelloReply> obs = new TestStreamObserver<>(); service.sayHello(request, obs); assertEquals("Hello Alice", obs.getValues().get(0).getMessage());
Dependencies are mocked with the usual tools (e.g. Mockito), and a lightweight test double implementing StreamObserver captures whatever is passed to onNext/onError/onCompleted for assertions - keeping the test fast and isolated from any real gRPC transport.
More Related questions...