如何正确使用android.test.ServiceTestCase?

如何正确使用android.test.ServiceTestCase?,android,android-service,android-testing,Android,Android Service,Android Testing,我的应用程序是在/system/app中运行的服务 com.abc.def.MyApp 尝试为它编写单元测试时,我在运行时在logcat中得到了这个错误 I/TestGrouping( 5647): TestCase class com.abc.def.test.MyAppTest is missing a public constructor with no parameters or a single String parameter - skipping 使用的命令: D:\tmp_i

我的应用程序是在/system/app中运行的服务

com.abc.def.MyApp
尝试为它编写单元测试时,我在运行时在logcat中得到了这个错误

I/TestGrouping( 5647): TestCase class com.abc.def.test.MyAppTest is missing a public constructor with no parameters or a single String parameter - skipping
使用的命令:

D:\tmp_install>adb shell am instrument -w com.abc.def.test/android.test.InstrumentationTestRunner
WARNING: linker: memtrack.jacinto6.so: unused DT entry: type 0xf arg 0x115
WARNING: linker: libsrv_um.so: unused DT entry: type 0xf arg 0xc4e
WARNING: linker: gralloc.jacinto6.so: unused DT entry: type 0xf arg 0x5d9
WARNING: linker: libpvr2d.so: unused DT entry: type 0xf arg 0x778

Test results for InstrumentationTestRunner=
Time: 0.0

OK (0 tests)
我的代码片段是:

public class MyAppTest extends ServiceTestCase<MyApp> {
public MyAppTest(Class serviceClass) {
      super(serviceClass);
      Log.d(tag, "hello world! MyAppTest ctor");
}
public MyAppTest() {
  super(com.abc.def.MyApp.class);
}
公共类MyAppTest扩展ServiceTestCase{
公共MyAppTest(类serviceClass){
超级(服务级);
Log.d(标记“helloworld!MyAppTest”);
}
公共MyAppTest(){
super(com.abc.def.MyApp.class);
}

我遵循了这个答案


我是否错误地使用了ServiceTestCase?

据我所见,您正确地使用了
ServiceTestCase

通常,您所需要的只是没有参数的构造函数,它才能正常工作,并且您已经提供了它

public MyAppTest() {
    super(com.abc.def.MyApp.class);
}
您收到的
链接器
警告通常与NDK连接。但它们对您的测试来说应该不是问题

虽然不是一个完整的解决方案,但我建议您尝试使用
gradle connectedAndroidTest
运行相同的测试。如果它们运行正常,至少我们知道问题出在
am仪器上,可能配置不正确

编辑:如果您使用
Gradle
构建项目(默认情况下,如果您使用的是Android Studio),只需进入您的主项目目录并执行以下命令,该命令将运行所有检测测试(如上面的
ServiceTestCase
示例)


正如Vesko所说,代码是正确的;但是问题的根本原因是Dex预优化——对代码的更新没有在运行时的行为中出现


解决方案?我必须将odex文件和apk推送到设备上。

我不知道为什么这个问题被否决。这不是链接器警告-它跳过了测试。如果你同意我的问题是有效的,那么请将问题向上投票,这样它会引起注意。有人将这个问题否决为否定。这确实是有效的问题。St我想知道为什么会完全跳过它?与Gradle相同?我不知道如何从Gradle运行。测试将在哪里运行?当前它们正在设备上安装,应该在设备上运行。检查我编辑的答案。调用此命令将生成应用程序,将其安装在连接的设备/模拟器上,并自动执行测试。详细信息如下:ed输出(结果)可以在app/build/reports/androidTests/I使用makefile构建项目下找到。
./gradlew connectedAndroidTest