title: How to mock Picasso in unit tests in Android?
body: <p>I have code where Picasso library is used.</p>
  
  <pre><code> @Inject
   public RegistrationPresenter(UserData userData, Picasso picasso) {
      this.userData = userData;
      this.picasso = picasso;
   }
  
   @Override
   public void attachView(RegistrationView view) {
      super.attachView(view);
      view.toggleKeyboardButton(false);
      view.showFirstName(userData.getFirstName());
      view.showLastName(userData.getLastName());
      view.showEmail(userData.getEmail());
      setImage();
   }
  
   private void setImage() {
      String url = String.format(IMAGE_URL, userData.getId());
      String tag = String.format(JPG_EXT, userData.getId());
      picasso
              .load(url)
              .tag(tag)
              .into(new Target() {
                  @Override
                  public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                      //some actions
                  }
  
                  @Override
                  public void onBitmapFailed(Drawable errorDrawable) {
                  }
  
                  @Override
                  public void onPrepareLoad(Drawable placeHolderDrawable) {
                  }
  
              });
  }
  </code></pre>
  
  <p>In my presenter I provide picasso via Dagger2</p>
  
  <pre><code>@Provides
  @Singleton
  Picasso providesPicasso() {
      return Picasso.with(context);
  }
  </code></pre>
  
  <p>In my tests I'm using Robolectric and Mockito, so I mock Picasso and pass it in presenter's constructor</p>
  
  <pre><code>@Mock
  Picasso picasso;
  
  @Before
  public void setUp() {
      super.setUp();
      MockitoAnnotations.initMocks(this);
      mockData = new UserData();
      mockData.setFirstName("Name");
      mockData.setLastName("LastName");
      mockData.setEmail("example@gmail.com");
      mockData.setId("123456");
  
      File mockCacheDir = mock(File.class);
      Context mockAppContext = mock(Context.class);
      when(mockContext.getApplicationContext()).thenReturn(mockAppContext);
      when(mockContext.getApplicationContext().getCacheDir()).thenReturn(mockCacheDir);
      when(view.getContext()).thenReturn(mockContext);
      presenter = new RegistrationPresenter(mockData, picasso);
  }
  
  @Test
  public void testAttachView() {
      presenter.attachView(view); //fails here
      verify(view).toggleKeyboardButton(false);
      verify(view).showFirstName(mockData.getFirstName());
      verify(view).showLastName(mockData.getLastName());
      verify(view).showEmail(mockData.getEmail());
  }
  </code></pre>
  
  <p>When I run my test, it fails with exception </p>
  
  <pre><code>java.lang.NullPointerException
  at com.squareup.picasso.Utils.createDefaultCacheDir(Utils.java:257)
  at com.squareup.picasso.OkHttpDownloader.&lt;init&gt;(OkHttpDownloader.java:45)
  at com.squareup.picasso.Utils$OkHttpLoaderCreator.create(Utils.java:424)
  at com.squareup.picasso.Utils.createDefaultDownloader(Utils.java:250)
  at com.squareup.picasso.Picasso$Builder.build(Picasso.java:832)
  at com.squareup.picasso.Picasso.with(Picasso.java:662)
  </code></pre>
  
  <p>What is proper way to mock Picasso?</p>