The Open Event project uses JSON format for transferring event information like tracks, sessions, microlocations and other. The event exported in the zip format from the Open Event server also contains the data in JSON format. The Open Event Android application uses this JSON data. Before we use this data in the app, we have to parse the data to get Java objects that can be used for populating views. There is a chance that the model and the JSON format changes in future. It is necessary that the models are able to parse the JSON data and the change in the model or JSON format don’t break JSON parsing. In this post I explain how to unit test local JSON parsing so that we can ensure that the models are able to parse the local JSON sample data successfully.
Firstly we need to access assets from the main source set into the unit test. There is no way to directly access assets from main source set. We need to first add assets in test/resources directory. If assets are present in test/resources directory then we can use it using ClassLoader in the unit test. But we can’t just copy assets from the main source set to resources directory. If there is any change in sample JSON then we need to maintain both resources and it may make the sample inconsistent. We need to make assets shared.
Add the following code in the app level build.gradle file.
It will add src/main/assets as a source directory for test/resources directory.So after building the project the test will have access to the assets.
Create readFile() method
Now create a method readFile(String name) which takes a filename as a parameter and returns data of the file as a string.
Here the getResourceAsStream() function is used to open file as a InputStream. Then we are creating byte array object of size same as inputStream data. Using read function we are storing data of file into byte array. After this we are creating a String object using a byte array.
Create ObjectMapper object
Create and initialize an ObjectMapper object in the Test class.
Here setting DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES to true is very important. It will fail the test for any unrecognized fields in the sample.
Create doModelDeserialization() method
In the Open Event Android App we are using Jackson for JSON serialization and deserialization. Converting JSON data to java is called deserialization or parsing.
Now create doModelDeserialization() method which takes three parameters,
- Class<T> type: Model Class type of the data
- String name: Name of the JSON data file
- boolean isList: true if JSON string contains the list of object else false
This method returns true if parsing is successful and false if there is any error in parsing.
Here ObjectMapper is doing the main work of parsing data and returns parsed object using readValue() method.
Now all the setup is done we just need to assert value returned by doModelDeserialization() method by passing appropriate parameters.
Here only event JSON file doesn’t have a list of the objects so passing false as isList parameter for others we are passing true because its data contains a list of objects.
Running unit tests after every build helps you to quickly catch and fix software regressions introduced by code changes to your app