2011-09-22 219 views
0

我有Android应用程序引用的纯Jar项目。 然后,我有我想创建单元测试类的常用函数的Functions.java。Android:单元测试:如何使用SensorManager创建单元测试?

这里是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; 
} 

在右侧方向上的任何指导理解。

回答

0

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

如果你真的想单元测试的逻辑,那么你需要重构,所以他们依赖于接口,然后构建模拟实现这些接口,所以你可以传递已知的数据和测试,如果你收到适当的输出