Skip to content

Creating new or updating old DirectionsRoute test fixtures

Dan Nesfeder edited this page Apr 26, 2018 · 1 revision

It's necessary that we have various DirectionsRoute objects, each continuing different information, that we can use to unit test our code.

Updating current fixtures

To do this, we set up a make command that allows us to call the Directions API and store the response in a .json file in our test resources.

To update our current fixtures, execute make navigation-fixtures in the terminal. You'll notice that the files (libandroid-navigation(-ui) > src > test > resources) are updated with new responses.

Creating a new fixture

To create a new fixture and load it into your test, you can do the following:

  • Add a new Directions API request to the Makefile with the data you are looking to test
# Example fixture description
curl "https://api.mapbox.com/directions/v5/mapbox/driving/-122.413165,37.795042;-122.433378,37.7727?access_token=$(MAPBOX_ACCESS_TOKEN)" \
    -o libandroid-navigation/src/test/resources/your_new_json_file_here.json
  • This is an example request and please note your_new_json_file_here.json
    • you will need to store your new response in a new json file. Create this prior to running the make command.

Loading it into your test

Now that you have the new data pulled in from the Directions API, you can load it into your unit tests. To do so, you can add this code to your test class:

private DirectionsRoute obtainADirectionsRoute() throws IOException {
    Gson gson = new GsonBuilder().registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create();
    String body = loadJsonFixture("your_new_json_file_here.json");
    DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class);
    DirectionsRoute aRoute = response.routes().get(0);
    return aRoute;
}

In this function, loadJsonFixture(String jsonFileName) is coming from the BaseTest that you can subclass when creating your new test class. This function will find the new data you created and let you convert it into a DirectionsRepsonse to be used for testing.

Happy testing! 🚀