gRPC test stub for unit and integration testing

Let’s imagine we have a gRPC service:

public class YourServiceImpl extends YourService.YourServiceImplBase {
  public void yourMethod() {
    // This is original realization

We are writing a unit test or integration test, and we need to make a test stub for this service. Let’s use Mockito. The code we write below can be used in any way. You can even use it in pure Java. Let’s connect the dependencies to the project:


Now, let’s mock our service:

YourService.YourServiceImplBase testStubService = mock(YourService.YourServiceImplBase.class, delegatesTo(
    new YourService.YourServiceImplBase() {
  void yourMethod() {
    // This is the realization for the test

It remains to run our test stub:

Server server = ServerBuilder
Telegram channel

If you still have any questions, feel free to ask me in the comments under this article or write me at

If I saved your day, you can support me 🤝

Leave a Reply

Your email address will not be published. Required fields are marked *