我需要创建一个伪造的位图映像以进行测试(JUNIT测试)我的个人添加并获取自定义linkedlist的方法,但是bitmap.createbitmap返回错误:
java.lang.runtimeException:Android.graphics.bitmap未嘲笑的方法createbitmap。
这是我的Junitest的代码:
public class TicketsIteratorTest {
Bitmap img_Bmp;
TicketsIterator<Bitmap> TicketsList = new TicketsIterator();
/*
* Test for the add e get methods, check if the element just insert it's the same of the one just extract.
*/
@Test
public void Add_n_Get() throws Exception {
int i = 0, numIMG = 100;
Bitmap[] IMG_Generated;
IMG_Generated = new Bitmap[numIMG];
// Generate numIMG of imagine to insert into the Iterator and it save each one of it into an
// Bitmap array usefull for testing of the get method
while (i <= numIMG) {
// Generation of the fake Ticket Bitmap
try {
img_Bmp = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
IMG_Generated[i] = img_Bmp;
} catch (Exception e) {
// Print the cause of the error just generated
e.getCause().printStackTrace();
}
// Addition of the imagine just created
TicketsList.add(img_Bmp);
i++;
}
// Test if the imagine inserted it is correct
while (i <= numIMG) {
assertTrue(IMG_Generated[i] == TicketsList.get(IMG_Generated[i]));
i++;
}
}
谢谢您的帮助。
您是否在常规的单元测试或Android测试中使用此?如果您从普通单元测试中调用此功能,则Android类将被空/空实现替换。这意味着致电bitmap.createbitmap()只会始终返回null而无需检查您的参数等。
我过去遇到了类似的问题。一切似乎都很好地工作了,除了后面什么都没有发生;)每当您看到类似的行为检查是否不是来自Android框架的类似行为 - 这很可能是出现问题的原因。
我希望就是那样,我可以帮助您。我正在使用PowerMock,只是模拟了位图类来返回我的情况下的位图模拟实例。
最好的问候
dariusz wiechecki
Instrumentation Test
情况将在这种情况下起作用, Unit test
案例将返回位图的null(Fail),因为它与Android框架有关。
我在Unit test
情况下读取连接设备(移动)filepath的文件时面临类似类型的问题,它无法阅读,但它在Instrumentation Test
您可以使用可绘制的kotlin扩展名tobitmap()。
contextCompat.getDrawable(context,r.drawable.x)?tobitmap()