title: How can I use OkHttpClient MockWebSever to return an image/byte[]?
body: <p>I'm working on an android library project, where I need to download an PNG image from a REST service, transform it into a <code>Bitmap</code> and return it to the app(s) using this library. So, we have a REST webservice that returns the bytes of a png image. We're calling this service using <code>Retrofit</code> with <code>rxJava</code>. In my code below <code>accessRemoteComm.getImage()</code> does the loading and transformation from <code>ResponseBody</code> to <code>Bitmap</code> inside a <code>.map</code> on the Observable. The image loads fine in the application. I now want to unit test that method, but I'm having a hard time getting <code>MockWebServer</code> to deliver the image in the first place. The <code>OnError</code> is called constantly with:</p>
  
  <p><code>java.lang.RuntimeException: Method decodeStream in android.graphics.BitmapFactory not mocked. See http://g.co/androidstudio/not-mocked for details.</code></p>
  
  <p>This is what I have so far:</p>
  
  <p><strong>Retrofit interface:</strong></p>
  
  <pre><code>@GET("webapi/user/{ID}/image")
  Observable&lt;ResponseBody&gt; getVehicleImage(
          @Path("ID") @NonNull final String id,
          @Query("width") @NonNull final int width,
          @Query("height") @NonNull final int height,
          @Query("view") @NonNull final ImageView view
  );
  </code></pre>
  
  <p><strong>getImage() Method:</strong></p>
  
  <pre><code>public Observable&lt;Bitmap&gt; getVehicleImage(@NonNull String id, @NonNull Integer width, @NonNull Integer height, @NonNull ImageView view) {
      return service.getImage(id, width, height, view).map(new Func1&lt;ResponseBody, Bitmap&gt;() {
          @Override
          public Bitmap call(ResponseBody responseBody) {
              BufferedInputStream isr = new BufferedInputStream(responseBody.byteStream());
              return BitmapFactory.decodeStream(isr);
          }
      });
  }
  </code></pre>
  
  <p><strong>My test method:</strong></p>
  
  <pre><code>@Test
  public void testGetVehicleImage() throws Exception {
  
      String path = basePathForImages + "vehicleTestImage.png";
  
      Source pngSource = Okio.source(new File(path));
  
      BufferedSource bufferedSrc = Okio.buffer(pngSource);
  
      server.enqueue(new MockResponse()
              .setResponseCode(200)
              .setHeader("Content-Type", "image/png")
              .setBody(bufferedSrc.buffer()));
  
      Subscriber&lt;Bitmap&gt; subscriber = new Subscriber&lt;Bitmap&gt;() {
          @Override
          public void onCompleted() {
              Log.d("OnComplete");
          }
  
          @Override
          public void onError(Throwable e) {
               Log.d(e.toString());
               //java.lang.RuntimeException: 
               //Method decodeStream in android.graphics.BitmapFactory not mocked.
          }
  
          @Override
          public void onNext(Bitmap bitmap) {
              Log.d("Yeeee");
          }
      };
  
      Observable&lt;Bitmap&gt; observable = accessRemoteCommVehicle.getVehicleImage("abc", 0, 0, VehicleImageView.FRONT);
  
      observable.toBlocking().subscribe(subscriber);
  }
  </code></pre>
  
  <p>I'm pretty sure, that I don't setup the <code>bufferedSource</code> correctly. But I cannot find any resources on SO or the web that shows the usage of the <code>MockResponse</code> with a <code>Buffer</code> as body.
  And this is the part, where any help is appreciated. How do I set this up correctly?</p>
  
  <p>Btw. If you have any other suggestion on how to test this, please let me know!</p>
  
  <p>Thank you!</p>