BigQuery Unit Testing in Isolated Environments
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:
- You need to unit test a function which calls on BigQuery (SQL,DDL,DML)
- You don’t actually want to run the Query/DDL/DML command, but just work off the results
- 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:
- Store BigQuery results as Serialized Strings in a property file, where the query (md5 hashed) is the key. (see RunSampleQuery.java and query.properties)
- In your unit test cases, mock BigQuery results to return from the previously serialized version of the Query output (see BigqueryTesting.java)