问题
I'm trying to make unit test for a function which receive android.location.Location object. But i blocked because i don't have possibility to set up parameters for Location object.
@Test
public void testLocation() {
Location location = new Location("gps");
location.setAccuracy(25F);
location.setTime(5123L);
System.out.println("Accuracy: " + location.getAccuracy());
System.out.println("Time: " + location.getTime());
}
Output:
Accuracy: 0.0
Time: 0
Process finished with exit code 0
Location accuracy and time are zero but the values were set in code above. It's not an Intrumentation test, i'm running it as simple unit test under jvm (Test Artifact: Unit Tests).
Using JUnit 4.12
回答1:
After adding @RunWith(RobolectricTestRunner.class) - it works as expected.
来源:https://stackoverflow.com/questions/35987519/set-parameters-of-android-location-location-object-from-junit-unit-test