0

我有简单的 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;
}

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

4

1 回答 1

0

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

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

于 2011-10-10T14:06:57.463 回答