我有一个在 textView 中打印位置变化的活动。我想用 JUnit 测试它。这是我的代码,这是我的输出:
public void testGPS() {
{
LocationManager lm = (LocationManager) myActivity
.getSystemService(Context.LOCATION_SERVICE);
String testProvider = "Test";
if (null == lm.getProvider(testProvider)) {
lm.addTestProvider(testProvider, false, false, false, false,
false, false, false, Criteria.POWER_LOW,
Criteria.ACCURACY_FINE);
}
lm.setTestProviderEnabled(testProvider, true);
lm.requestLocationUpdates(testProvider, 0, 0,
myActivity.mLocListener);
lm.setTestProviderStatus(testProvider, LocationProvider.AVAILABLE,
null, System.currentTimeMillis());
// getService().setUpProvider(testProvider,lm)
Location location = new Location(testProvider);
location.setLatitude(1.0);
location.setLongitude(2.0);
location.setTime(System.currentTimeMillis());
lm.setTestProviderLocation(testProvider, location);
String test_rs = "lon=" + location.getLongitude() + "\nlat="
+ location.getLatitude();
String msg = "Location Succeeded";
// Assert.assertFalse("Received Location is null", received == null
// );
assertEquals(msg, myActivity.mTextLocation.getText().toString(),
test_rs);
lm.removeTestProvider(testProvider);
}
跟踪是:
junit.framework.ComparisonFailure: Location Succeeded expected:<Unknown> but was:<lon=2.0
lat=1.0>
at mypackage.TestActivity.testGPS(TestActivity.java:73)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
at android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:186)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)
所以你可以在这里看到:
junit.framework.ComparisonFailure: Location Succeeded expected:<Unknown> but was:<lon=2.0 lat=1.0>.
所以就像 myMain Activity 没有从我的测试中获取位置。
有什么帮助吗?
编辑: myActivity 是我要测试的活动。第 73 行是断言。我只想通过测试将位置传递给活动。怎么做?