Back to Blog Archive

Mock Database Connector With Dataweave, Enabling Test Driven Development

Posted on: October 25, 2018

This blog post outlines how to use Dataweave to easily mock database connectors in MUnit, enabling test driven development for database based system APIs.

Test driven development (TDD) is a development approach whereby tests for an application are defined and written before any code has been implemented, requiring the developer to thoroughly think through the expected behaviour beforehand and therefore reducing rework. In order to write tests for database system APIs in MUnit, we need to be able to mock the responses that would be returned from queries.

Result sets from the database we would like to mock are readily exportable as CSVs, so we will use these as our mock responses. In our case we store these in the class path at src/test/resources/mocks. Once the data is in CSV format, we lose any metadata denoting the data types that each field should be. Therefore we have inserted a line underneath the column names to specify the data type that each column should contain. Another issue we face is that null strings cannot be distinguished from empty strings. To resolve this we add the keyword <empty_string> where we would expect to find an empty string. Here is an example where we mock a call to a table holding user information, saving the file as users.csv:

The following Dataweave code reads a CSV file from the class path such as the one above, and returns a List of HashMaps with each field cast to the datatype specified in the column, replicating a database component response. This should be saved as a .dwl file in the class path (src/test/resources).

The functions are explained as follows:

  • getCsvFile simply reads the CSV file from the class path
  • toType takes a string value and casts it to the type defined
  • csvToJava maps each line of data in the CSV file to a HashMap, where the first line is used for the keys and the second line is used for the data type casting via toType

With the database-mock.dwl and users.csv saved to the class path, we can now mock the response in an MUnit test. With the following configuration for a mock component, we can mock a database component named ‘Select Users’. We utilise the MEL function dw to be able to use the Dataweave readUrl function to read database-mock.dwl and invoke the exposed csvToJava. You can read more about the uses of the readUrl function in this blog post.

When running the test in debug mode we see that the CSV provided above yields the following Java object, where we can see that the correct data types have been assigned.

Click on the image to enlarge.

Once a test is written, development can start with the sole aim of passing that test. This starts a process of incrementally adding and improving tests, using these as acceptance criteria for development. This helps reduce bugs by breaking the project into smaller modules to focus on and complete. The process relies on many transitions between testing and development, which the above method facilitates by providing an easy and intuitive way of mocking the database connectors.



Contact Us

Ricston Ltd.
Triq G.F. Agius De Soldanis,
Birkirkara, BKR 4850,
MT: +356 2133 4457
UK: +44 (0)2071935107

Send our experts a message

Need Help?
Ask our Experts!