我正在尝试实例化一项服务,用于测试我们正在整合的位置系统。基本上,我接受了这项服务:http://developer.android.com/training/location/location-testing.html并将相关的类导入我的项目中。我想要的是一个后台服务,它将在运行时将模拟位置传播到所有LocationClient
。
我项目的清单如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="air.com.sourceco.mobile.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="18" />
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="air.com.sourceco.mobile" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library android:name="android.test.runner" />
<service android:name="com.sourceco.mobile.location.test.SendMockLocationService"
android:exported="true" android:label="@string/app_name">
<intent-filter>
<action android:name="com.sourceco.mobile.location.test.SendMockLocationService"></action>
</intent-filter>
</service>
</application>
</manifest>
我在Eclipse中有两个项目 - 我的原始Sourceco
项目,它具有我想要测试的功能,以及我的SourcecoTest
项目,它具有JUnit测试。以下是我拥有的课程(TestLocation
,SendMockLocationService
和LocationUtils
几乎逐字逐句从Google网站上复制,但已将其更改为不同的测试位置。
LocationHelperTest.java
(我正在使用的主要课程,在com.sourceco.mobile.location.test
包中):
package com.sourceco.mobile.location.test;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.test.AndroidTestCase;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.sourceco.mobile.location.GPLocationServicesConsumer;
public class LocationHelperTest extends AndroidTestCase implements ConnectionCallbacks {
private GPLocationServicesConsumer mConsumer;
// According to the documentation, we should always set the provider to "flp",
// which is the location provider code that Location Services puts into the
// Location objects it sends out.
public static final String PROVIDER = "flp";
// Latitude and Longitude of:
// Captain Tony's Saloon, 428 Greene Street, Key West, FL 33040
public static final double LAT = 24.558760;
public static final double LON = -81.805434;
public static final float ACCURACY = 10.0f;
private Intent mServiceIntent;
@Override
public void setUp() {
// Start our service for mock location processing
mServiceIntent = new Intent("com.sourceco.mobile.location.test.SendMockLocationService");
mContext.startService(mServiceIntent);
mConsumer = new GPLocationServicesConsumer(mContext);
mConsumer.notifyActivityCreated();
mConsumer.notifyActivityStarted();
}
@Override
public void tearDown() {
mContext.stopService(mServiceIntent);
}
public Location createLocation(double lat, double lon, float acc) {
Location newLocation = new Location(PROVIDER);
newLocation.setLatitude(lat);
newLocation.setLongitude(lon);
newLocation.setAccuracy(acc);
return newLocation;
}
public void testLocationRetrieval() throws InterruptedException {
// Arrange
Location mockLocation = createLocation(LAT, LON, ACCURACY);
// Act
Location testLocation = mConsumer.getLocation();
// Assert
assertEquals("Latitudes should be equal", mockLocation.getLatitude(), testLocation.getLatitude());
assertEquals("Longitudes should be equal", mockLocation.getLongitude(), testLocation.getLongitude());
assertEquals("Accuracies should be equal", mockLocation.getAccuracy(), testLocation.getAccuracy());
}
@Override
public void onConnected(Bundle arg0) {
}
@Override
public void onDisconnected() {
}
}
但是,当我运行测试时,我得到以下内容:
java.lang.RuntimeException: Unable to instantiate service
com.sourceco.mobile.location.test.SendMockLocationService:
java.lang.ClassNotFoundException: Didn't find class
"com.sourceco.mobile.location.test.SendMockLocationService" on path:
DexPathList[[zip file "/system/framework/android.test.runner.jar",
zip file "/data/app/air.com.sourceco.mobile.test-2.apk"],
nativeLibraryDirectories=[/data/app-lib/air.com.sourceco.mobile.test-2,
/vendor/lib, /system/lib]]
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2556)
at android.app.ActivityThread.access$1800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class
"com.sourceco.mobile.location.test.SendMockLocationService" on path:
DexPathList[[zip file "/system/framework/android.test.runner.jar",
zip file "/data/app/air.com.sourceco.mobile.test-2.apk"],
nativeLibraryDirectories=[/data/app-lib/air.com.sourceco.mobile.test-2,
/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2553)
... 10 more
最初,我认为这是由谷歌播放服务没有正确导入引起的,但它是在原始的Sourceco项目中导入的,而这似乎并不是它所抱怨的。相反,它抱怨它无法找到SendMockLocationService
的类定义。
知道这里会发生什么吗?我不太清楚为什么这项服务不会启动我的测试。