how to correctly use android.test.ServiceTestCase?

2019-05-10 06:42发布

My app is a Service that runs in /system/app

com.abc.def.MyApp

Trying to write unit tests for it, I get this error in the logcat while running.

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

command used:

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)

My snippet of code is:

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);
}

....

I have followed this answer https://stackoverflow.com/a/8981508/398348

Am I using ServiceTestCase incorrectly?

2条回答
We Are One
2楼-- · 2019-05-10 07:12

From what I see, you're using ServiceTestCase correctly!

Generally all you need is the constructor with no parameters for it to work correctly and you've provided it.

public MyAppTest() {
    super(com.abc.def.MyApp.class);
}

The linker warnings you get are usually connected with the NDK. Still they shouldn't be a problem for you tests.

Although not a complete solution, I suggest you try running the same tests with gradle connectedAndroidTest. If they run properly, at least we know the problem is with the am instrument - perhaps not correctly configured TEST project.

EDIT: If you're using Gradle to build your project (by default if you're using Android Studio), just go into your main project directory and execute the following command, which will run all your Instrumentation tests (like the ServiceTestCase example above).

./gradlew connectedAndroidTest
查看更多
走好不送
3楼-- · 2019-05-10 07:31

As Vesko said, the code is correct; however root cause of the problem was Dex preOptimization - updates to my code were not showing up in the behavior of the runtime.

Solution? I had to push the odex file as well as the apk to the device.

查看更多
登录 后发表回答