Jitesh Upadhyay
Jitesh Upadhyay

Reputation: 5260

Robolectric custom shadow code

Hi everytime i am getting this exception trace with Robolectric while working on custom shadows

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at org.robolectric.bytecode.RobolectricInternals.newInstanceOf(RobolectricInternals.java:33)
    at org.robolectric.Robolectric.newInstanceOf(Robolectric.java:345)
    at org.robolectric.shadows.ShadowBitmapFactory.create(ShadowBitmapFactory.java:120)
    at org.robolectric.shadows.ShadowBitmapFactory.decodeFile(ShadowBitmapFactory.java:72)
    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java)

what i am doing is i have a custom shadow

@Implements(Bitmap.class)
class MyShadowBitmap extends org.robolectric.shadows.ShadowBitmap {

    public MyShadowBitmap() {
        // can also be some other config value
        setConfig(Bitmap.Config.ARGB_8888);
    }

}

and i am using this class

public class CustomTestRunner extends RobolectricTestRunner {
    public CustomTestRunner(Class<?> testClass) throws InitializationError {
        super(testClass);
    }
    @Override
       public Setup createSetup() {
           return new MySetup();
       }
    @Override
    protected ShadowMap createShadowMap() {
        return super.createShadowMap()
                .newBuilder()
                .addShadowClass(MyShadowBitmap.class)

                .build();
    }
    }
}

and also i am running my test cases as

@Test
    @Config(shadows = {
        MyShadowBitmap.class
    })

please help me that where i am doing wrong and how to work with custom shadows in robolectric!!

Upvotes: 2

Views: 2689

Answers (2)

jiahao
jiahao

Reputation: 3393

I am quite new on Robolectric also, but I made a shadowImageView which works. You might have a look on this code: https://github.com/jiahaoliuliu/RobolectricSample/tree/roboMockitoTutorial

Errors you should fix:

  1. You might have the config over the declaration of class, instead of the test.
  2. All the shadowed methods should have the @Implementation notation
  3. The constructor cannot be shadowed

Here is the code of my Shadow Class, extract from the Robolectric web page:

package com.jiahaoliuliu.robolectricsample;

import android.graphics.Bitmap;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.shadows.ShadowBitmap;

import java.io.OutputStream;

/**
 * Created by jiahao on 2/15/15.
 */
@Implements(Bitmap.class)
public class MyShadowBitmap extends ShadowBitmap {
    @RealObject
    private Bitmap realBitmap;
    private int bitmapQuality = -1;

    @Implementation
    public boolean compress(Bitmap.CompressFormat format, int quality, OutputStream stream) {
        bitmapQuality = quality;
        System.out.println("Using the shadow to compress");
        return true;
    }
}

Good luck!

Upvotes: 1

snowdragon
snowdragon

Reputation: 753

I'm pretty new to robolectric myself, but I think you need to define the constructor for the shadow differently, like:

public void __constructor__() and not the usual public MyShadowBitmap()

See here: http://robolectric.org/extending/ (Shadowing Constructors section)

Upvotes: 1

Related Questions