Annyce Davis

ones and zeros

  • Home
  • Public Speaking
  • About Me
  • Courses
  • Life

Testing Tricks #3: Third Party APIs

December 14, 2015 by Annyce Davis Leave a Comment



When you’re writing unit tests for your Android applications, you will often need to handle interactions with third party APIs. In the above image we’re using the Picasso Image Loading library in our classes. Picasso has a very clean API, so it’s relatively simple to work with; but with other libraries you may not be so fortunate. So what can we do to simplify our interactions with those APIs and also set ourselves up for more maintainable code in the future?

We can encapsulate the calls to Picasso (or whatever API) into another class that allows us to control the API and make assertions against it in our tests. So here’s an example:

Initial Code

// MyClass class

public void loadImageFromUrl(String imageUrl) {
    presenter.setCurrentPreviewImageUrl(imageUrl);
    if (showThumbImage) {
        Picasso.with(context)
               .load(imageUrl)
               .into(thumbImageView);
    }
}

So in this method, loadImageFromUrl, we want to make sure that our Presenter is being called and that when the showThumbImage variable is set to true, we call the method to load the image. We want to do this of course, without actually loading the image since we’re attempting to verify this behavior inside of a unit test.

For our use case we will need two new classes: ImageLoader and ImageLoaderImpl. The first will be an interface that we will use to expose the desired API, and the second will be a class that implements that interface and wraps the calls to the third party API.

Updated Code

// ImageLoader interface

public interface ImageLoader {

    void loadImage(String imageUrl, ImageView imageView);

}

// ImageLoaderImpl class

public class ImageLoaderImpl implements ImageLoader {
  
    // fields omitted

    @Override
    public void loadImage(String imageUrl, ImageView imageView) {
        Picasso.with(context).load(imageUrl).into(imageView);
    }
}

// loadImageFromUrl method

public void loadImageFromUrl(String imageUrl) {
    presenter.setCurrentPreviewImageUrl(imageUrl);
    if (showThumbImage) {
        imageLoader.loadImage(imageUrl, thumbImageView);
    }
}

Unit Test

// MyClass

@Mock private ImageLoader imageLoader;

@Test
public void shouldLoadImageWhenShowThumbIsTrue() throws Exception {
    myClass.setShowThumbImage(true);

    myClass.loadImageFromUrl("http://www.myimageurl.png");

    verify(imageLoader).loadImage(anyString(),
                                  any(ImageView.class));
}

@Test
public void shouldNotLoadImageWhenShowThumbIsFalse() throws Exception {
    myClass.setShowThumbImage(false);

    myClass.loadImageFromUrl("http://www.myimageurl.png");

    verifyZeroInteractions(imageLoader);
}
 
So in these two unit tests we are able to successfully verify the interactions with our Image Loader and we don’t have to be concerned with what’s going on inside our third party API. What’s more, if we decide in the future that we would like to use another Image Loading library, we’ve encapsulated those changes in one place!


How do you like to unit test third party APIs? Please leave a comment below.

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)

Related

Filed Under: Android Tagged With: Unit Testing

Follow Me

What engineering leaders need to know from this year’s Google I/O

I didn't intentionally aim to create a rhyming title, but there you have it. Each year, I look forward to Google I/O like a kid going back to school. This year … [Read More...]

Talk: The Real MVP

You have an idea for a new app. You've secured buy-in from the business. What's next? The MVP. But what does it take to ship a new app these days? What are the … [Read More...]

Categories

  • Android (55)
  • Career (2)
  • Communication (4)
  • Flutter (1)
  • Git (4)
  • Gradle (4)
  • Grails (23)
  • Java (8)
  • JavaScript (6)
  • Kotlin (17)
  • Life (4)
  • Public Speaking (23)
  • RxJava (1)
  • Software Development (7)
  • Twitter (3)
  • Uncategorized (11)
  • Video Course (5)

Copyright © 2023 · Beautiful Pro Theme on Genesis Framework · WordPress · Log in