【问题标题】:How to mock BitmapFactory: Method decodeFile如何模拟 BitmapFactory:方法 decodeFile
【发布时间】:2019-03-28 23:52:51
【问题描述】:

我有一个程序可以从存储区中保存的全尺寸图像创建缩略图。我正在尝试使用 mockito 测试该功能,但它给了我以下错误:

java.lang.RuntimeException:android.graphics.BitmapFactory 中的方法 decodeFile 未被模拟

//已解决(更新代码)

我第一次使用 mockito 运行单元测试,有人可以建议我做错了什么(我知道肯定会这样做)。我也在使用 ExifInterface 来提取与图像关联的元数据,但它再次给了我同样的错误: java.lang.RuntimeException:android.media.ExifInterface 中的方法 getAttribute 未被模拟。

这是 MainActivity 类:(我在其中运行该方法)。

 public class MainActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            initValue();
        }

        public void initValue()
        {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            Bitmap thumbnail = createThumbnailFromBitmap("/storage/emulator/0/demo/abcd", 100, 100);

 try {
            ExifInterface exifInterface = new ExifInterface("/storage/emulator/0/demo/abcd");
            String jsonData = exifInterface.getAttribute("UserComment");

            try {
                JSONObject rootJson = new JSONObject(jsonData);
                dateList.add(rootJson.getString("captured"));

            }

            catch(JSONException e)
            {
            }
        }
        catch(Exception e)
        {
            System.out.println("exception "+e);
        }
        }

        private Bitmap createThumbnailFromBitmap(String filePath, int width, int height){
            return ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(filePath), width, height);
        }

    }

我的测试班:

 @RunWith(PowerMockRunner.class)
@PrepareForTest({BitmapFactory.class ,ThumbnailUtils.class})
    public class initValueTest {


        @Mock
        private Bitmap bitmap;


        @Test
    public void initValueTest()
    {
        PowerMockito.mockStatic(BitmapFactory.class);
        PowerMockito.mockStatic(ThumbnailUtils.class);
        when(BitmapFactory.decodeFile(anyString())).thenReturn(bitmap);
        MainActivity mainActivity =  new MainActivity();
        mainActivity.initValue();
    }
    }

感谢你们的帮助。如果我做错了什么,请原谅。

【问题讨论】:

    标签: android unit-testing mockito powermockito


    【解决方案1】:

    您可以:

    • 使用 power mock 来模拟静态方法 decodeFile。请参阅有关它的码头here
    • 将位图解码逻辑提取到单独的类中,提取接口并提供 运行时的不同实现。

    【讨论】:

    • 您好,非常感谢您的快速解决方案。我希望我能提供的不仅仅是标记为已接受的答案并且有用。非常感谢。我使用了 PowerMockito.mockStatic(BitmapFactory.class);模拟静态方法并且它起作用了。
    • 所以,这部分是成功的,我在那个文件中有更多的代码,它们实际上使用 ExifInterface 从图像中读取元数据。我为 ExifInterface 做了同样的 mockStatic,但它又给了我同样的错误:Method getAttribute in android.media.ExifInterface not mocked
    • 正如我在代码中看到的那样。 getAttribute 方法不是静态的,所以它不会那样工作。您需要为 exifinterface 实例提供完整的模拟并模拟它的方法。
    • 你能写一个基于它的示例代码吗,我不知道如何完全模拟那个类。再次感谢您的帮助。
    • 感谢您的回复,对我帮助很大。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多