【问题标题】:Android: Unit Test: How can I create unit test with SensorManager?Android:单元测试:如何使用 SensorManager 创建单元测试?
【发布时间】:2011-09-22 07:26:49
【问题描述】:

我有简单的 Jar 项目,由 android 应用程序引用。 然后,我有 Functions.java 用于我想要创建单元测试类的常用函数。

这是 Functions.java 中的示例函数:

public static double getAltitudeBySensor(float atmosphericPressure) {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD
            && atmosphericPressure > 0d) {
        return SensorManager.getAltitude(SensorManager.PRESSURE_STANDARD_ATMOSPHERE, atmosphericPressure);
    }
    return 0d;
}

public static double toPixelX(double imageSize, android.location.Location upperLeft, android.location.Location lowerRight, android.location.Location target) {
    double hypotenuse = upperLeft.distanceTo(target);
    double bearing = upperLeft.bearingTo(target);
    double currentDistanceX = Math.sin(bearing * Math.PI / OneEightyDeg) * hypotenuse;
    //                           "percentage to mark the position"
    double totalHypotenuse = upperLeft.distanceTo(lowerRight);
    double totalDistanceX = totalHypotenuse * Math.sin(upperLeft.bearingTo(lowerRight) * Math.PI / OneEightyDeg);
    double currentPixelX = currentDistanceX / totalDistanceX * imageSize;

    return currentPixelX;
}

感谢任何有关正确方向的指导。

【问题讨论】:

    标签: android unit-testing android-sensors


    【解决方案1】:

    这两种方法都依赖于外部代码,因此难以进行单元测试。

    如果您真的只想对逻辑进行单元测试,那么您需要重构它们以使它们依赖于接口,然后构建实现这些接口的模拟,这样您就可以传入已知数据并测试您是否收到了适当的输出

    【讨论】:

      猜你喜欢
      • 2015-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-21
      • 2012-01-08
      • 1970-01-01
      • 2021-10-02
      • 1970-01-01
      相关资源
      最近更新 更多