Java JUnit测试Android Studio中的Bitmap.CreateBitmap返回null
我需要创建一个假位图图像,用于测试(JUnit测试)自定义LinkedList的个人添加和获取方法,但bitmap.createBitmap返回错误: java.lang.RuntimeException:未模拟android.graphics.Bitmap中的方法createBitmap 这是我的JUnitTest的代码:Java JUnit测试Android Studio中的Bitmap.CreateBitmap返回null,java,android,junit,bitmap,createbitmap,Java,Android,Junit,Bitmap,Createbitmap,我需要创建一个假位图图像,用于测试(JUnit测试)自定义LinkedList的个人添加和获取方法,但bitmap.createBitmap返回错误: java.lang.RuntimeException:未模拟android.graphics.Bitmap中的方法createBitmap 这是我的JUnitTest的代码: public class TicketsIteratorTest { Bitmap img_Bmp; TicketsIterator<Bitmap&
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++;
}
}
public class tickets迭代测试{
位图img_Bmp;
TicketsIterator TicketsList=新的TicketsIterator();
/*
*测试add-e-get方法,检查元素是否刚刚插入,它是否与刚刚提取的元素相同。
*/
@试验
public void Add\n\u Get()引发异常{
int i=0,numIMG=100;
生成位图[]IMG_;
IMG_Generated=新位图[numIMG];
//生成imagine的numg以插入到迭代器中,并将其保存到
//位图数组用于测试get方法
而(i您是在常规单元测试中还是在Android测试中使用此选项?如果您是在普通单元测试中调用此选项,Android类将替换为空/null实现。这意味着调用Bitmap.createBitmap()将始终返回null,甚至不检查您的参数等
我在过去的位图和Base64OutputStream中也遇到过类似的问题。除了后面没有发生任何事情之外,一切似乎都运行得很好;)每当你看到类似的行为时,检查类是否不是来自Android框架,这很可能就是问题的原因
我希望是这样,我可以帮助你们。在我的例子中,我正在使用PowerMock和刚刚模拟的位图类来返回位图模拟实例
致以最良好的祝愿
Dariusz Wiechecki仪器测试
案例将在这种情况下工作,单元测试
案例将为位图返回null(失败),因为它与Android框架相关
在
单元测试
的情况下,从连接设备(移动设备)文件路径读取文件时,我遇到了类似的问题,它无法读取,但在仪器测试
中工作,我在Matteo搜索相同的查询