Android: Unit Test: How can I create unit test with SensorManager?
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 d开发者_开发知识库irection 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
精彩评论