How to mock gRPC api for integration testing with golang

mock grpc server java
golang integration testing
golang mock interface
unit test grpc server c
grpc dialcontext example
grpc check if server is running
grpc contract testing


I have a service written in go which communicates with a couple of other services via gRPC protocol. I want to write integration tests for my service but, I have no possibility to use real dependencies.

So, I need to mock the dependencies in some way.

For me, a perfect scenario looks like:

  • I can run a mock-server and pass its address instead of a real dependency to my application
  • I can register expected response for a specific call to the dependant service
  • I can reset the mock-server to tear down the data after a test case.

I tried to use but it doesn't support all the protobuff syntax.

What can help with my problem? Maybe I'm completely wrong and there are best practices for integration testing of services communicating gRPC

You can spin up your environment by defining all of your services in a docker-compose file. You should add the service you want to test as well in the docker-compose file. After spinning it up you can run your integration tests in the service you want to test.

Example docker-compose file:

version: '2.2'

    build: .
    image: service-you-want-to-test
    container_name: service-you-want-to-test
      - REDIS_ADDRESSES=[redis:6379]
      - SERVICE_YOU_DEPEND_ON_ENDPOINT=service-you-depend-on:8091
      - DB_HOST=mysql
      - DB_USER=root
      - redis
      - mysql
      - service-you-depend-on
        condition: service_healthy
      - 8090:8090

    image: service-you-depend-on:latest
    container_name: service-you-depend-on
      - DB_HOST=mysql
      - DB_USER=root
      - redis
      - mysql
        condition: service_healthy
      - 8091:8091

    image: mysql:5.7
    container_name: mysql
    - ./.docker/mysql/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
    - ./.docker/mysql/healthcheck-5.7:/var/www/healthcheck
      test: "/var/www/healthcheck"
      interval: 5s
      timeout: 5s
      retries: 5
    - 3306:3306

    image: redis:3.2
    container_name: redis
      - 6379:6379

To set up your integration environment you can use these commands:

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go
docker build -t service-you-want-to-test .
docker-compose up -d redis mysql
docker-compose up -d --build service-you-want-to-test

After running the above you can just execute your integration tests via the command line:

go test -v path/to/integration_tests/...

So you should add the services that you depend on to the docker-compose file as well so you can actually send a request to them in your integration tests. Be sure to add those service endpoints in your environment variables so you can override the endpoint you send your request to.

For resetting your data in for example your database you can write fixtures. The fixtures are just helper functions to reset the data of your database, cache, etc.

Testing a gRPC Service in Go With Table Driven Tests, Thanks to this magefile, we do not have to execute all the extended commands that will allow us to generate the mocks, test the static files, run the  One of the huge benefits of using gRPC is the ability to autogenerate our client and server stubs from the protocol buffer definitions. In the same way, we can we generate our own code by building a plugin for the protocol buffer compiler (protoc). The plugin we're creating is going to auto-generate gRPC response messages so that we can build a mock gRPC server in Go (Golang). Goal Given the

Another alternative tool that you can use in this situation is Traffic Parrot.

We recently added support for gRPC to our service virtualization tool which includes a user interface that can be used to define the mock behaviour.

There is a tutorial on how to mock gRPC responses over the wire given a proto file.

You can also find information on how to record and replay over the wire in the documentation.

Mocking grpc services and tests in golang - Allen Ng, We all having hard time when we try to mock external services involving network calls, we somehow need to overwrite the grpc client so it can  I have gRPC service ( say svc1) which invokes another gRPC service ( using its gRPC stub say svc2_client) I have generated client mock for svc2_client, and unit test as per documentation is working

I just merge some patch for covering most of protobuf declaration. I suggest you check on that.

known issue: it still can't parse protobuf that has method body declaration

grpc-go/ at master, Split examples & Integration Test (#27) If you already familiar with Apiary or WireMock for mocking API service and looking for similiar thing for GRPC then this is the We gonna use Docker image for easier example test. basic syntax to run GripMock is gripmock <protofile> Example for go: go run example/client/go​/*.go. The Go language implementation of gRPC. HTTP/2 based RPC - grpc/grpc-go

tokopedia/gripmock: gRPC Mock Server, The title of this blog post might appear weird for those in the Ruby and Golang communities. Rarely do you hear of developers writing an API in  gRPC Mock . A gRPC Java testing utility tool to easily mock endpoints of gRPC services. The tool follows a similar DSL type of structure to HTTP mocking service WireMock. Features. gRPC method stubbing configurable through a fluent Java API; Headers and request body matchers to determine the correct stub; Configurable delay for individual responses

Testing a Golang gRPC API with RSpec, Mocking and simulating gRPC APIs using service virtualization This introduces an issue: how can the client teams test their client system against the Let's go through an example of how to simulate a gRPC service that is specified in the  Testing bidirectional rpc on golang. Ask Question Asked 1 year, 3 months ago. Active 1 year, How to mock gRPC api for integration testing with golang. 0.

Chapter 5: Mocking and simulating gRPC, GRPC server. is a little bit trickier to test. Here's an example (it won't compile, but the main idea is this):. This post is a technical presentation on how we use gRPC (and Protobuf) to build a robust client/server system. I won’t go into the details of why we chose gRPC as the main communication protocol…

  • You have a service you want to test. I would expect that you start the service in your test with a test configuration and then you make requests to the service with a client (your test client) then you compare that data you got with the expected data. The idea of an integration test is not to mock something, then it won't be an integration test anymore.
  • Sorry for the wrong terminology. I need the black box tests for the business logic in my service. The problem is that my service has only a few dependencies, but the dependencies require a lot of infrastructures. Think it as a microservice which uses only one handler from a monolith
  • Presumably you have access to the proto definitions, right? So generate the service stubs and implement them however you'd like. If memory serves, protoc generates client and server code together anyway, so you should already have the server interfaces.
  • Did you ever solve this issue? Struggling to get this done with gripmock too, and suspecting some of the stuff I'm trying to do might be unsupported