Main content

Testing BBC iPlayer: reading XML, JSON response into Java

Gaurav Kumar


Tagged with:

My name is Gaurav Kumar. I am a Developer In Test in the BBC TV iPlayer team.

I have been with the BBC for just over four months and I work in the iPlayer Business Logic area. I work on the layer that makes sense of the data we receive and puts it into a usable format for iPlayer front-end. This data includes things like scheduling, categories, channel highlights and everything else that drives the front end.

The testing around this is very important as it spans not only two separate teams within Programmes and On Demand but also drives the heart of the iPlayer experience for the end users. This takes a lot of coordination and planning to make sure we capture the changes that come through.

My team works using BDD ("behaviour drive development") and I am co-located with the developers. Within the team we automate as much as we can to avoid manually repeatable tasks but we do also have some manual testing that we run.

This blog-post is based on my experience of testing in iPlayer Business Layer, that is testing a RESTful API. Instead of telling you about the basic principles and mechanics of API testing, I want to talk about a very important aspect of automating API testing, that is reading and verifying the response in XML/JSON format.

Testing of a RESTful API is mainly about calling the web services that return a response and then verifying the data returned in response. The response can either be in XML or JSON format.

I have been writing the automated tests in Java by utilizing its built-in libraries so it’s quite obvious that this post will cover no other language but Java.

First let’s consider an example of a XML response:

In Java there are different ways and XML parsers available to read XML response. I am going to show a very simple and easy way of doing it via DOM XML parser. Let’s create a custom method to make our job easier.

Now we have a method, which can be used to read the value from nodes. Consider that we get the previously shown example of xml response as the response when an API is being called.

Please Note: Here, I am assuming that you already know how to get the API response in one way or another as I am not covering that topic in this blog-post.

Suppose the response body is obtained and is passed to a List URI Variable apiResponse, then we can get the value of any node using the method getNodeValues e.g.

getNodeValues("//title", apiResponse.getContent());

will return the values “FirstExample”, “SecondExample”

Similarly (“//rating”, apiResponse.getContent());

will return the values “100”, “120”

Simple enough?

Now let’s deal with JSON response.

The example I am going to work through is about salaries of people working in a fictional IT department.

Consider the following as the JSON example:

Again there are different methods and parsers to read the JSON response but I find JSON Path as one simplest ways to parse JSON response. In fact it is being said that what XPath is for XML, JSON Path is for JSON.

We need the following import:

import com.jayway.jsonpath.JsonPath;

Now if we want the salary of all staff, we can pass:

To get staff from the IT dept.

Once you are able to get the response from XML or JSON file formats, you can then either assert the values to expected values or use them in other parts of your tests.

I hope you find this post helpful.

Gaurav Kumar is a developer, Test, Programmes and On Demand, BBC Future Media

Tagged with:

More Posts


What's on BBC Red Button - 25-31 Jan