문제

I have plain Jar Project that reference by android application. Then, I have Functions.java for my common functions which I want to create unit test class.

Here are the sample functions inside the 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;
}

Any guidance on the right direction is appreciated.

도움이 되었습니까?

해결책

Both methods have dependencies on external code so will be difficult to unit test.

If you really want to unit test just the logic then you'll need to refactor so they depend on interfaces and then build mocks that implement those interfaces so you can pass in known data and test if you receive the appropriate output

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top