在Android测试中模拟设备数据

时间:2015-08-06 14:38:34

标签: android unit-testing mocking

运行我的Android应用程序的设备现在可以将其数据发送到服务器。但是我在测试时遇到了问题,因为设备的数据会因设备而异。

以下是我获取某些设备数据的方法:

    String simSerialNumber = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE)).getSimSerialNumber();
    String androidVersion = Build.VERSION.RELEASE;
    String deviceModel = Build.MODEL;
    String deviceSerial =  Build.SERIAL;

我如何在测试中模拟这些?

1 个答案:

答案 0 :(得分:2)

找到了一种非常方便的方法来模拟此帖子中的设备信息: https://groups.google.com/forum/#!topic/robolectric/-d4aaAGB7Ks

如果您使用Roboelectric,您可以使用ReflectionHelpers类。

df <- structure(list(V1 = structure(1:2, .Label = c("A001", "A003"), 
      class = "factor"),V2 = structure(1:2, .Label = c("A002", "A004"), 
      class = "factor")), .Names = c("V1","V2"), class = "data.frame", 
      row.names = c(NA, -2L))
相关问题