BigQuery Unit Testing in Isolated Environments

Ajay Prabhakar
Aug 16, 2021

--

Making BigQuery unit tests work on your local/isolated environment that cannot connect to BigQuery APIs is challenging. This article describes how you can stub/mock your BigQuery responses for such a scenario

The scenario for which this solution will work:

  1. You need to unit test a function which calls on BigQuery (SQL,DDL,DML)
  2. You don’t actually want to run the Query/DDL/DML command, but just work off the results
  3. You want to run several such commands, and want the output to match BigQuery output format

The code available here: https://github.com/hicod3r/BigQueryUnitTesting and uses Mockito https://site.mockito.org/

The idea in a nutshell:

  1. Store BigQuery results as Serialized Strings in a property file, where the query (md5 hashed) is the key. (see RunSampleQuery.java and query.properties)
  2. In your unit test cases, mock BigQuery results to return from the previously serialized version of the Query output (see BigqueryTesting.java)

--

--