2

我有一个在 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 行是断言。我只想通过测试将位置传递给活动。怎么做?

4

1 回答 1

2

我已经设法让它发挥作用,至少对我来说是这样。

线索在这里:https ://developer.android.com/training/location/location-testing.html

...您不必在发布之前禁用或删除测试代码。

... 和 ...

始终将提供程序值设置为 flp,这是位置服务放入它发出的位置对象中的代码。

我还发现有必要给更新时间提供交付。

所以,这就是我最终得到的结果:

public void testGPS() {
    LocationManager lm = (LocationManager) mActivity
        .getSystemService(Context.LOCATION_SERVICE);

    lm.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
    lm.setTestProviderStatus(LocationManager.GPS_PROVIDER, 
                             LocationProvider.AVAILABLE,
                             null, System.currentTimeMillis());

    Location location = new Location(LocationManager.GPS_PROVIDER);
    location.setLatitude(1.0);
    location.setLongitude(2.0);
    location.setTime(System.currentTimeMillis());
    lm.setTestProviderLocation(LocationManager.GPS_PROVIDER, location);

    try {
        Thread.sleep(2000);
    } catch(InterruptedException e) {
    }
}
于 2014-04-27T13:05:10.680 回答