嗨,每次我在处理自定义阴影时使用Robolectric获得此异常跟踪
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)
我正在做的是我有一个自定义阴影
@Implements(Bitmap.class)
class MyShadowBitmap extends org.robolectric.shadows.ShadowBitmap {
public MyShadowBitmap() {
// can also be some other config value
setConfig(Bitmap.Config.ARGB_8888);
}
}
我正在使用这个课程
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();
}
}
}
我也在运行我的测试用例
@Test
@Config(shadows = {
MyShadowBitmap.class
})
请帮助我,我在做错了,以及如何在robolectric中使用自定义阴影!!
答案 0 :(得分:1)
我自己对robolectric很陌生,但我认为你需要以不同方式定义阴影的构造函数,例如:
public void __constructor__()
而不是通常的public MyShadowBitmap()
见这里: http://robolectric.org/extending/(影子构造函数部分)
答案 1 :(得分:1)
我对Robolectric也很新,但是我制作了一个有效的shadowImageView。您可以查看此代码: https://github.com/jiahaoliuliu/RobolectricSample/tree/roboMockitoTutorial
您应该修复的错误:
以下是我的Shadow Class的代码,摘自Robolectric网页:
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;
}
}
祝你好运!