How to mock dgraph for unit test

Hi Dgraph team,

We are using Dgraph in our project so we come up with question how to mock dgraph in unit tests?


We use docker-compose to run a 6-node Dgraph cluster, and our integration tests use that cluster. See:

1 Like

First of all, are you talking about doing tests on Dgraph itself or unit testing on your project?

If so, what language do you use?

All Clients created by Dgraph have some testing approach. As the example that was mentioned by Mrjn. I think you can get a lot done by exploring the test folders and analyzing how the Mocks are made.

In practice mocks and approaches will depend a lot on your unit test framework. Dgraph works just like any DB. So technically the tests should be very common.

Also if you’re using JS, In this link below I have an example that might help you create something (for code unit test). It’s creating a “Seed” using JSON File to add data. But the test approach you can create a similar function. Where you use “Drop-all” for each test level.

Also Dgraph-js and dgraph-js-http has good test samples and uses jest.


Dgo client " examples_test.go"

Pydgraph client




Thank you so much @mrjn for the quick reply. Actually, my tests are unit tests. so I wonder how to test functions that use dgraph in unit tests.

I could use your approach in my integration tests. is there no way to write unit tests without running dgraph docker?


@MichelDiz thanks for the reply. I was talking about unit tests of my project that uses dgraph. I am using Scala language.
I have a method signature like,

def get(query: String, vars: util.Map[String, String]): DgraphProto.Response

Here now i want to mock DgraphProto.Response. I can use any mocking framework like java mockito


I have no idea how I would do with Scalar, however as I said it should be quite common. You can download the Dgraph binaries in Then you will put them in a suitable path.

You will need to create a kind of “Life cycle” to ascend and kill the Dgraph instances used for testing. In the hypothesis of not using Docker. By docker it would be much simpler.

Why do not you use the HTTP endpoint? You will have a simplified JSON response. Are you using any 3rd party Scalar client?

Dgraph has a Java Client- Dgraph4j. See how the tests are done.