Unit Testing Json Result data in WinRT applications | Derik Whittaker


If you are building applications today which access data via any type of web based API it is very likely you going to be consuming data which is formatted as Json. When you get your data back you are almost certainly going to be manipulating this data in some way.  You may just read out some values directly, or you may want to deserialize the data to some sort of object graph.

If you are a developer which believes in unit testing you are going to want to test this process in order to ensure that you are always doing things the right way.

When it comes time to the consumption and conversion of your Json you have 2 choices:

  1. Have integration tests which make a web call out to the endpoint to consume the data on the fly.  — This is not something I suggest doing if at all possible
  2. Create snapshots of your Json data and write unit tests which work directly against this snapshot — This is what I suggest doing

When you are building WinRT applications things can be a bit harder or more effort than typical applications.  Although it may be harder, it is not that hard and is pretty easy once you get the hang of it.

Lets take a look at how we accomplish this in a WinRT application

1: Need to create and store a snapshot of your Json file

What I like to do is create a simple text file and put it into a folder structure as seen below.  You should also notice that I like to follow the naming convention of XXX.json.txt.


2: Creating a helper class to load to load the raw Json

Once I have my files on disk I like to create a very simple helper class which allows me to access the underlying json with no effort. I use the helper class because I do not want to clutter my tests with this logic because I assume I may be loading this data many, many times.

private const string BaseFilePath = "JsonSerialization/json/";
public async static Task DirectorySearchJson()

    return await ReadFile(BaseFilePath + "DirectorySearchResults.json.txt");

private async static Task ReadFile(string fileName)
    var jsonFile = Path.Combine(Package.Current.InstalledLocation.Path, fileName);

    var jsonFolder = await StorageFolder.GetFolderFromPathAsync(Path.GetDirectoryName(jsonFile));

    using (var jsonStream = await jsonFolder.OpenStreamForReadAsync(Path.GetFileName(jsonFile)))
        using (var streamReader = new StreamReader(jsonStream))
            var json = streamReader.ReadToEnd();
            return json;

If you look at the code above you will see 2 methods.  The first method (public one) simply calls the ReadFile method and provides it the full path to the file.  Please understand that this file is the relative file based on the location of your test class to the actual json.txt file.

In the 2nd method I am doing all the heavy lifting of accessing, opening and reading the file.

3: Using this in a unit test

Once we have access to our file we can very simply use it inside our unit tests.  Do do this all we need to do is something like you see below. (I chose not to make the test async for no other reason than i am lazy).

public async Task Can_Create_Contact_Info_Email_Section_Correctly()
    var json = await FileLoaders.DirectorySearchJson();
    var searchResponse = JsonConvert.DeserializeObject(json);

Now that we can store, read and use a json.txt files there is no logic reason why we would not create unit tests to make sure our translations work every time

Till next time,