gpt4 book ai didi

android - Robolectric 自定义影子代码

转载 作者:行者123 更新时间:2023-11-29 21:10:32 25 4
gpt4 key购买 nike

嗨,每次我在处理自定义阴影时使用 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 中使用自定义阴影!!

最佳答案

我自己是 robolectric 的新手,但我认为您需要以不同方式定义阴影的构造函数,例如:

public void __constructor__() 而不是通常的 public MyShadowBitmap()

看这里: http://robolectric.org/extending/ (阴影构造函数部分)

关于android - Robolectric 自定义影子代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23009826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com